java 我们如何让一个线程在java中无限期地休眠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30845546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:44:39  来源:igfitidea点击:

How can we make a thread to sleep for a infinite time in java?

javamultithreadingthread-sleep

提问by Logesh

Pls help me to understand how can we make a thread to sleep for a infinite time period .

请帮助我了解如何让线程无限期地休眠。

回答by user1121883

I can't think of a good reason for doing this. As one of the comments noted Long.MAX_VALUE is roughly 292 billion years so probably Thread.sleep(Long.MAX_VALUE)is enough. But if you want a theoretical infinite sleep solution:

我想不出这样做的充分理由。正如评论之一指出 Long.MAX_VALUE 大约是 2920 亿年,所以可能Thread.sleep(Long.MAX_VALUE)就足够了。但是如果你想要一个理论上的无限睡眠解决方案:

while (true) {
    Thread.sleep(Long.MAX_VALUE);
}

回答by Stephen C

Literally, you can't. No Java application can run for an infinite amount of time. The hardware will die first :-)

从字面上看,你不能。没有任何 Java 应用程序可以无限运行。硬件将首先死亡:-)

But in practice1, the following will sleep until the JVM terminates ... or the thread is interrupted.

但在实践1 中,以下将休眠直到 JVM 终止......或线程被中断。

 public void freeze() throws InterruptedException {
    Object obj = new Object();
    synchronized (obj) {
        obj.wait();
    }
 }

If you wanted to you could catch the exception within a while (true)loop. And doing the same with "sleep(max int)" is equivalent.

如果您愿意,您可以在while (true)循环中捕获异常。对“sleep(max int)”做同样的事情是等价的。

But frankly, making a thread go to sleep "for ever" is wasteful2, and probably a bad idea. I have no doubt that there will be better ways to achieve what you are reallytrying to do.

但坦率地说,让线程“永远”休眠是浪费2,而且可能是个坏主意。我毫不怀疑会有更好的方法来实现您真正想做的事情。



1 - These solutions I talk about are for when a thread needs to make itselfgo to sleep. If you one thread to unilaterallymake a different thread go to sleep, it can't do that safely. You could use the deprecated Thread.suspend()method, but it is dangerous, and it may not be available on future Java platforms.

1 - 我谈论的这些解决方案是在线程需要让自己进入睡眠状态时使用的。如果您让一个线程单方面让另一个线程进入睡眠状态,则无法安全地做到这一点。您可以使用已弃用的Thread.suspend()方法,但它很危险,并且在未来的 Java 平台上可能无法使用。

2 - A thread stack occupies a significant amount of memory, and it cannot be released until the thread terminates.

2 - 线程堆栈占用大量内存,并且在线程终止之前无法释放。

回答by Taylor Gautier

Thread.currentThread().join();

Will sleep until the JVM is killed.

将休眠直到 JVM 被杀死。

回答by k13i

You can use class CyclicBarrierfrom the JDK.

您可以使用JDK 中的CyclicBarrier类。

new CyclicBarrier(2).await();

Constructor argument is the number of threads that must invoke awaitmethod before the barrier is reached.

构造函数参数await是到达屏障之前必须调用方法的线程数。

回答by JFPicard

Make it wait for a mutex or resource that will never be released. It's the deadlock principle. The better way is to make the thread to finish his code, so it will end and not be started again.

让它等待永远不会被释放的互斥锁或资源。这就是死锁原理。更好的方法是让线程完成他的代码,这样它就会结束并且不会再次启动。

Edit: I don't recommand an infinite loop since it's the pooling principle. It will consume a lot of resources for nothing.

编辑:我不推荐无限循环,因为它是池化原则。它会白白消耗大量资源。