--有什么区别?

Thread workerThread = new Thread(runnable);
workerThread.start();
workerThread.sleep(5000);
.....


Thread workerThread = new Thread(runnable);
workerThread.start();
Thread.sleep(5000);

它们都导致工作线程暂停睡眠吗?

谢谢

最佳答案

没有 sleep是不会影响当前线程的静态方法:http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)

所以没有区别,他们俩都不会做你想做的。不建议在实例上使用静态方法,因为它会使您认为您可以执行实际上无法执行的操作(如本例所示)

小添加-请参见John对why calling static methods from class instances is allowed in Java :-)的回答

08-15 20:13