简体   繁体   English

有没有办法让所有正在运行的线程在Java中变为sleep()?

[英]Is there a way to make all running threads sleep() in Java?

It's all in the title, really. 确实是标题中的所有内容。 I know Thread.sleep() only makes the current thread sleep, but is there a way to force all threads to sleep simultaneously, or would that be down to self-management? 我知道Thread.sleep()仅使当前线程进入睡眠状态,但是有没有一种方法可以强制所有线程同时进入睡眠状态,还是只能依靠自我管理?

It would be dangerous to put thread into sleep from outside, since it can be currently in some intermediate state (that's the reason why it's not good to kill thread from outside, for example). 从外部使线程进入睡眠状态将很危险,因为它可能当前处于某种中间状态(例如,这就是为什么从外部杀死线程不好的原因)。 So please make it via self-management. 因此,请通过自我管理来实现。

The answer is simple : there is no way to make all the thread sleep from Java (from the inside of the application). 答案很简单:没有办法使所有线程都从Java(从应用程序内部)进入休眠状态。

Moreover, on Java, there is a lot of internal thread (like GC thread, Finalization thread, ... ) that you cannot manipulate so "all" will never be possible. 而且,在Java上,有很多内部线程(如GC线程,Finalization线程等)您无法操纵,因此“ all”将永远不可能。

Regards, 问候,

Loïc 洛伊奇

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM