小编One*_*ree的帖子

在等待另一个线程时让当前线程休眠

在我的应用程序的某个时刻,我想让我的主线程(即当前正在执行的线程)休眠一段时间或直到后台完成(并唤醒它),以先到者为准。

这是我所做的(我认为会起作用,但没有)

public static void main(String args[])
{
    // .... other stuff ...

    // show a splash screen
    // just think of this as an image
    showPlashScreen(): 
    new Thread(new Runnable()
    {
          public void run()
          {
                // do some work here

                // notify everyone after the work is done
                Thread.currentThread().notifyAll();
          }
    }).start();

    // now make the current thread to wait for the background
    // or for at most 1000
    Thread.currentThread().wait(1000);
    disposeSplashScreen();

    // ... other stuff ....
}
Run Code Online (Sandbox Code Playgroud)

执行这个,我不断得到 java.lang.IllegalMonitorStateException

(部分)堆栈跟踪:

Caused …
Run Code Online (Sandbox Code Playgroud)

java multithreading notify wait

0
推荐指数
1
解决办法
6815
查看次数

为什么Java选择这种重载超过另一种

任何人都可以向我解释为什么Java选择第二次重载而不是第一次?

public static void foo (int a, double b, double...c) {}
public static void foo (double...a) {}
public static void bar ()
{
    // this is the second
    foo(1);
}
Run Code Online (Sandbox Code Playgroud)

我认为当我1作为参数传递时,Java会选择第一个参数,因为int它更具体而不是double

谢谢

java overload-resolution

0
推荐指数
1
解决办法
41
查看次数

名字应该是英文好吗?

(这可能是提出问题的错误地方,请告诉我).

我应该说出我的方法isStaticallyImported还是isStaticlyImported?(我相信它们的发音方式大致相同)

java naming-conventions

-1
推荐指数
1
解决办法
100
查看次数

这段代码如何合法java?

我最近意识到你可以做到这一点(至少在Java 1.7+中).

static interface MyInter {
    int foo = 3; 
}
static {
    System.out.println(MyInter.foo);
}
Run Code Online (Sandbox Code Playgroud)

这究竟是怎么合法的?java编译器只是变成int foo场景后面的最终静态字段吗?

(如果是这样,那么,哇,Java变得越来越疯狂了!)

java

-1
推荐指数
1
解决办法
195
查看次数