java中如何让线程休眠特定时间?
我有一个场景,我希望线程休眠特定的时间。
代码:
public void run(){
try{
//do something
Thread.sleep(3000);
//do something after waking up
}catch(InterruptedException e){
// interrupted exception hit before the sleep time is completed.so how do i make my thread sleep for exactly 3 seconds?
}
}
现在,我如何处理我尝试运行的线程在睡眠完成之前遇到中断异常的情况?另外,线程在被中断后是否会唤醒并进入可运行状态,或者什么时候只有在进入可运行状态后流程才会进入 catch 块?
I have a scenario where i want a thread to sleep for specific amount of time.
Code:
public void run(){
try{
//do something
Thread.sleep(3000);
//do something after waking up
}catch(InterruptedException e){
// interrupted exception hit before the sleep time is completed.so how do i make my thread sleep for exactly 3 seconds?
}
}
Now how do i handle the case where the thread i am trying to run is hit with an interrupted exception before the complete of the sleep? Also does the thread wake up after being interrupted and does it go to runnable state or when is it that only after it goes to runnable does the flow go to catch block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我是这样使用的:
所以不需要等待特定时间结束。
I use it this way:
So it is not necessary to wait the specific time to end.
为什么你想睡正好3秒?如果只是需要在一段时间后执行某些操作,请尝试使用 计时器。
Why do you want to sleep for exactly 3 seconds? If it's just having to execute something after some time, try using a Timer.
当您的线程遇到中断时,它将进入 InterruptedException catch 块。然后,您可以检查线程休眠了多少时间,并计算出还有多少时间可以休眠。最后,最好不要吞掉异常,而是恢复中断状态,以便调用堆栈上方的代码可以处理它。
When your thread is hit by an interrupt it will go into the
InterruptedException
catch block. You can then check how much time the thread has spent sleeping and work out how much more time there is to sleep. Finally, instead of swallowing the exception, it is good practice to restore the interruption status so that code higher up the call stack can deal with it.根据此页面,您必须对其进行编码才能表现出来你想要的方式。使用上面的线程你的睡眠将被中断并且你的线程将退出。理想情况下,您应该重新抛出异常,以便启动线程的任何内容都可以采取适当的操作。
如果你不希望这种情况发生,你可以将整个事情放在 while(true) 循环中。现在,当中断发生时,睡眠被中断,您会吃掉异常,然后循环以开始新的睡眠。
如果您想完成 3 秒的睡眠,可以通过进行 10 次 300 毫秒的睡眠并将循环计数器保持在 while 循环之外来近似。当你看到中断时,吃掉它,设置一个“我必须死”标志,然后继续循环,直到你睡够了。然后以受控方式中断线程。
这是一种方法:
According to this page you'll have to code it to behave the way you want. Using the thread above your sleep will be interrupted and your thread will exit. Ideally, you'd re-throw the exception so that whatever started the thread could take appropriate action.
If you don't want this to happen, you could put the whole thing in a while(true) loop. Now when the interrupt happens the sleep is interrupted, you eat the exception, and loop up to start a new sleep.
If you want to complete the 3 seconds of sleep, you can approximate it by having, say, 10 sleeps of 300 milliseconds, and keeping the loop counter outside a while loop. When you see the interrupt, eat it, set a "I must die" flag, and continue looping until you have slept enough. Then you interrupt the thread in a controlled manner.
Here's one way:
试试这个:
如果你自己不中断睡眠,为什么这个线程会被唤醒?看来你做了一些非常错误的事情...
try this:
if you do not interrupt the sleep yourself, why would this thread be awoken ? is seems that you are doing something very wrong...