Java 让线程休眠 30 分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24879303/
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
Making a Thread to Sleep for 30 minutes
提问by venkata lokesh
I want to make my thread to wait for 30 minutes. Are there any problems of doing this?
我想让我的线程等待 30 分钟。这样做有什么问题吗?
回答by sampathsris
You can make your thread sleep for 30 minutes like this:
你可以让你的线程像这样休眠 30 分钟:
Thread.sleep(30 * // minutes to sleep
60 * // seconds to a minute
1000); // milliseconds to a second
Using Thread.sleep
is not inherently bad. Simply explained, it just tells the thread scheduler to preempt the thread. Thread.sleep
is bad when it is incorrectly used.
使用Thread.sleep
本身并不坏。简单解释一下,它只是告诉线程调度程序抢占线程。Thread.sleep
使用不当是不好的。
- Sleeping without releasing (shared) resources: If your thread is sleeping with an open database connection from a shared connection pool, or a large number of referenced objects in memory, other threads cannot use these resources. These resources are wasted as long as the thread sleeps.
- Used to prevent race conditions: Sometimes you may be able to practically solve a race condition by introducing a
sleep
. But this is not a guaranteed way. Use a mutex. See Is there a Mutex in Java? As a guaranteed timer: The sleep time of
Thread.sleep
is not guaranteed. It could return prematurely with anInterruptedException
. Or it could oversleep.From documentation:
public static void sleep(long millis) throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
- 在不释放(共享)资源的情况下休眠:如果您的线程正在使用共享连接池中打开的数据库连接或内存中的大量引用对象休眠,则其他线程无法使用这些资源。只要线程休眠,这些资源就会被浪费。
- 用于防止竞争条件:有时您可以通过引入
sleep
. 但这不是一个有保证的方式。使用互斥锁。请参阅Java 中有互斥锁吗? 作为有保证的计时器:
Thread.sleep
不保证的睡眠时间。它可能会以InterruptedException
. 或者它可能会睡过头。从文档:
public static void sleep(long millis) throws InterruptedException
使当前正在执行的线程休眠(暂时停止执行)指定的毫秒数,取决于系统计时器和调度程序的精度和准确性。
You could also use, as kozla13has shown in their comment:
您也可以使用,正如kozla13在他们的评论中所示:
TimeUnit.MINUTES.sleep(30);
回答by isnot2bad
The answer of Krumia already perfectly shows how to sleep a running Thread
. Sometimes, the requirement to sleep or pause a thread originates from the wish to perform an operation at a later date. If that's the case, you should better use a higher level concept like Timer
or ScheduledExecutorService
:
克鲁米亚的回答已经完美地展示了跑步的方法Thread
。有时,睡眠或暂停线程的要求源于希望在以后执行操作。如果是这种情况,您最好使用更高级别的概念,例如Timer
or ScheduledExecutorService
:
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(operation, 30, TimeUnit.MINUTES);
Where operation
is the Runnable
you want to execute in 30 minutes.
哪里operation
是Runnable
你想在30分钟内执行。
Using a ScheduledExecutorService
, you can also execute operations periodically:
使用 a ScheduledExecutorService
,您还可以定期执行操作:
// start in 10 minutes to run the operation every 30 minutes
executor.scheduleAtFixedDelay(operation, 10, 30, TimeUnit.MINUTES);