简体   繁体   English

java中的线程中断

[英]Thread Interruption in java

I want some clarification on thread interruption. 我想要一些关于线程中断的澄清。

What if a thread goes a long time without invoking a method that throws anInterruptedException? 如果一个线程长时间没有调用抛出anInterruptedException的方法怎么办? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. 然后它必须定期调用Thread.interrupted,如果收到中断,则返回true。 For example: 例如:

for (int i = 0; i < inputs.length; i++) {
    heavyCrunch(inputs[i]);
    if (Thread.interrupted()) {
        // We've been interrupted: no more crunching.
        return;
    }
}

When I call the method Thread.interrupt() it throws interrupted exception so, why I need to do if (Thread.interrupted()) I will simply do that 当我调用方法Thread.interrupt()它会抛出中断的异常,所以,为什么我需要这样做if (Thread.interrupted())我会简单地这样做

try {
    for (int i = 0; i < inputs.length; i++) {
        heavyCrunch(inputs[i]);
        if (Thread.interrupted()) {
            // We've been interrupted: no more crunching.
            return;
        }
    }
} catch (InterruptedException ex) {
        ...
}

When you call Thread.interrupt() on another thread, two things happen: 当你在另一个线程上调用Thread.interrupt()时,会发生两件事:

  1. That thread's interrupt flag is set. 该线程的中断标志已设置。
  2. If the thread is blocked in a method that throws InterruptedException , that method will throw an exception immediately. 如果线程在throws InterruptedException的方法中被阻塞,则该方法将立即抛出异常。

If the thread is busy in some code that does not throw InterruptedException , you won't get any exception. 如果线程忙于某些不会抛出InterruptedException代码,则不会出现任何异常。 That's why you'd need to check if (Thread.interrupted()) , to check the flag. 这就是为什么你需要检查if (Thread.interrupted())来检查标志。

In your sample code the Java compiler would complain about an invalid catch block because none of the code in the try block throws InterruptedException . 在您的示例代码中,Java编译器会抱怨无效的catch块,因为try块中没有任何代码会抛出InterruptedException

Because Thread.interrupted() , as documented , doesn't throw InterruptedException at all. 因为Thread.interrupted()如文档所述 ,根本不会抛出InterruptedException It returns true or false, and clears the interrupt status that has been set by the call to Thread.interrupt() . 它返回true或false,并清除由Thread.interrupt()调用设置的中断状态。 Note that Thread.interrupt() doesn't cause an InterruptedException to be thrown either It simply sets the interrupt status. 请注意, Thread.interrupt()不会引发InterruptedException ,它只是设置中断状态。

Calling interrupt on a thread does not directly cause the thread to throw an InterruptedException. 在线程上调用中断不会直接导致线程抛出InterruptedException。 All it does is set the interrupt flag for that thread. 它所做的就是为该线程设置中断标志。 Methods like sleep, wait, or join can reference that flag on the current thread in order to decide whether to throw an InterruptedException. sleep,wait或join等方法可以在当前线程上引用该标志,以决定是否抛出InterruptedException。

Interruption is cooperative, the thread being interrupted decides how it will respond; 中断是合作的,被中断的线程决定它将如何响应; this way the thread can do whatever cleanup it needs to do and not get shot down partway through. 通过这种方式,线程可以执行它需要执行的任何清理操作,而不会在中途被击落。

You should usually use Thread.isInterrupted() because that doesn't change the value of the interrupted flag. 您通常应该使用Thread.isInterrupted(),因为它不会更改中断标志的值。 Thread.interrupted() resets the flag: Thread.interrupted()重置标志:

The interrupted status of the thread is cleared by this method. 此方法清除线程的中断状态。 In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it). 换句话说,如果要连续两次调用此方法,则第二次调用将返回false(除非当前线程在第一次调用已清除其中断状态之后且在第二次调用之前检查它时再次中断)。

while Thread.isInterrupted() doesn't: Thread.isInterrupted()不:

Tests whether this thread has been interrupted. 测试此线程是否已被中断。 The interrupted status of the thread is unaffected by this method. 线程的中断状态不受此方法的影响。

Checking the interrupted flag means your thread can detect whether it's been interrupted without its having to sleep or wait. 检查中断标志意味着您的线程可以检测它是否在没有睡眠或等待的情况下被中断。 Neither Thread.isInterrupted() nor Thread.interrupted() throws InterruptedException, the code using it would check the return value. Thread.isInterrupted()和Thread.interrupted()都不会抛出InterruptedException,使用它的代码会检查返回值。

Also, Thread.interrupt doesn't throw InterruptedException. 此外,Thread.interrupt不会抛出InterruptedException。 (And any exception thrown would be in the calling thread, not in the thread being interrupted.) The interrupt method is used to set the interrupt flag on another thread. (并且抛出的任何异常都将出现在调用线程中,而不是在被中断的线程中。)interrupt方法用于在另一个线程上设置中断标志。 Having a thread interrupt itself is pointless, the thread could return instead. 线程中断本身是没有意义的,线程可以返回。

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

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