静态休眠线程与使用实例 [英] Sleeping a thread statically versus using its instance
本文介绍了静态休眠线程与使用实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
--
Thread workerThread = new Thread(runnable);
workerThread.start();
workerThread.sleep(5000);
.....
还有
Thread workerThread = new Thread(runnable);
workerThread.start();
Thread.sleep(5000);
它们都导致工作线程暂停睡眠吗?
Do they both cause worker thread to pause sleep?
谢谢
推荐答案
No sleep is a static method that affects the current thread: http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)
所以没有区别,他们俩都不会做你想做的.不推荐在实例上使用静态方法,因为它使您认为您可以执行实际上无法执行的操作(如本例所示)
So there is no difference, they both won't do what you want. The use of static methods on an instance is deprecated discouraged since it make you think you can do something you actually can't (like in this case)
Small addition - see John's answer to why calling static methods from class instances is allowed in Java :-)
这篇关于静态休眠线程与使用实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文