if Thread.sleep is static, how does individual thread knows it is put to sleep?

女生的网名这么多〃 提交于 2019-12-01 16:20:20

Does it mean Thread.sleep() puts the current Thread to sleep?

Yes. Only the current thread can do that.

What if t1 wants to put t to sleep. that shouldn't be possible correct?

Right. You can set a volatile boolean flag that will cause another thread to call Thread.sleep(...) but another thread can't cause a thread to sleep.

 volatile boolean shouldSleep = false;
 ...
 // check this flag that might be set by another thread to see if I should sleep
 if (shouldSleep) {
     Thread.sleep(...);
 }

It can find the current thread from Thread.currentThread(). The current thread only can put itself to sleep.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!