Java 中断或停止休眠线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4264355/
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
Interrupting or stopping a sleeping thread
提问by Suresh
How to stop or interrupt a sleeping thread in java.? I have a thread that syncs data and sleeps for 10 minutes in run() method, if i want to stop the sync by stopping the thread when it is sleeping.? How can this be achieved?
如何停止或中断java中的休眠线程。?我有一个线程可以同步数据并在 run() 方法中休眠 10 分钟,如果我想通过在线程休眠时停止线程来停止同步。?如何做到这一点?
回答by Fredrik Wallenius
Call the interrupt() method on your thread. This will cause the sleep to be cancelled and an InterruptedException will be thrown.
在您的线程上调用 interrupt() 方法。这将导致睡眠被取消并抛出 InterruptedException。
回答by EboMike
You can interrupt a sleeping thread with Thread.interrupt()
. Now stopping a thread is deprecated - what if the thread is holding a lock to something? (There was a Thread.stop()
, but it is HIGHLY discouraged).
您可以使用 中断休眠线程Thread.interrupt()
。现在停止线程已被弃用 - 如果线程持有某个东西的锁怎么办?(有一个Thread.stop()
,但非常不鼓励)。
Alternatively, instead of sleeping, you can have your thread wait for an object to be notified with a timeout, and simplify notify the object if you want the thread to wake up.
或者,你可以让你的线程等待一个对象被超时通知,而不是休眠,如果你想要线程唤醒,则简化通知对象。
回答by AlexR
Frederik is right: call Thread.interrupt(); I just wanted to warn you not to usestop() as it is deprecated since java 1.1. And other warning. I think that if you are using sleep() and wish to interrupt the thread it is a good moment to think about move to wait() and notify(). There are many tutorials about java threads and the javadoc of java.lang.Thread is good enough, so you can continue reading there if you are not familiar with these APIs.
Frederik 是对的:调用 Thread.interrupt(); 我只是想警告您不要使用stop(),因为它自 java 1.1 以来已被弃用。以及其他警告。我认为,如果您正在使用 sleep() 并希望中断线程,那么现在是考虑转向 wait() 和 notify() 的好时机。关于java线程的教程很多,java.lang.Thread的javadoc已经很不错了,不熟悉这些API的可以继续阅读。
回答by Alexandr
Suresh, I wish to pay attention to one issue. interrupt() methods itself does not interrupt thread. It just sends a request to a thread for interruption, by setting the flag to true. Usually your thread should process cancellation/interruption policy. It is greatly described in Java Concurrecy in Practice, section 7.1 Task Cancellation.
苏雷什,我想关注一个问题。interrupt() 方法本身不会中断线程。它只是通过将标志设置为 true 来向线程发送中断请求。通常你的线程应该处理取消/中断策略。它在 Java 并发实践中的第 7.1 节任务取消中有详细描述。
Blocking library methods such Thread.sleep and Object.wait try to detect when a thread has been interrupted and return early. They respond to interruption by clearing the interrupted status and throwing InterruptedException.
Thread.sleep 和 Object.wait 等阻塞库方法尝试检测线程何时被中断并提前返回。它们通过清除中断状态并抛出 InterruptedException 来响应中断。
回答by GuiRitter
Since I can't comment, I'll post another answer. I just want to reinforce and clarify what Alexandr said. interrupt()
only sets a flag in the Thread
, and the extended Thread
or Runnable
object have to check if it have been interrupted with Thread.interrupted()
to do what it's supposed to do when interrupted.
由于我无法发表评论,我将发布另一个答案。我只是想加强和澄清亚历山大所说的话。interrupt()
只在 中设置一个标志Thread
,并且扩展Thread
或Runnable
对象必须检查它是否已被中断,Thread.interrupted()
以便在被中断时执行它应该执行的操作。
interrupt()
also stops a wait()
or sleep()
, but pay attention that those methods UNSET the interrupt flag. So, if you call interrupt()
on a sleeping/waiting thread, it will stop sleeping/waiting but will not know that it was interrupted, except if you catch the exception, and will not continue to be interrupted.
interrupt()
也停止 a wait()
or sleep()
,但要注意那些方法 UNSET 中断标志。所以,如果你调用interrupt()
一个睡眠/等待线程,它会停止睡眠/等待,但不会知道它被中断了,除非你捕捉到异常,并且不会继续被中断。
I was able to fix a program of mine by implementing my own interrupt flag, which couldn't be overridden by the sleep()
and wait()
methods. Also, know that you can interrupt a thread from within with Thread.currentThread().interrupt()
.
我能够通过实现我自己的中断标志来修复我的程序,该标志不能被sleep()
和wait()
方法覆盖。另外,要知道您可以从内部中断线程Thread.currentThread().interrupt()
。