在我的应用程序的某个时刻,我想让我的主线程(即当前正在执行的线程)休眠一段时间或直到后台完成(并唤醒它),以先到者为准。
这是我所做的(我认为会起作用,但没有)
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选择第二次重载而不是第一次?
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?
谢谢
(这可能是提出问题的错误地方,请告诉我).
我应该说出我的方法isStaticallyImported还是isStaticlyImported?(我相信它们的发音方式大致相同)
我最近意识到你可以做到这一点(至少在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变得越来越疯狂了!)