java 在 Thread.join() 之前调用 Thread.interrupt() 是否会导致 join() 立即抛出 InterruptedException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10632451/
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-10-31 01:54:59  来源:igfitidea点击:

Does calling Thread.interrupt() before a Thread.join() cause the join() to throw an InterruptedException immediately?

javamultithreadingconcurrencyinterruptinterrupted-exception

提问by X?pplI'-I0llwlg'I -

Basically, what the question title says.

基本上,问题标题说的是什么。

Thread t = new Thread(someRunnable);
t.start();
t.interrupt();
t.join(); //does an InterruptedException get thrown immediately here?

From my own tests, it seems to, but just wanted to be sure. I'm guessing Thread.join()checks the interruptedstatus of the thread before doing its "wait" routine?

从我自己的测试来看,似乎可以,但只是想确定一下。我猜在执行“等待”例程之前Thread.join()检查interrupted线程的状态?

回答by Gray

Does calling Thread.interrupt() before a Thread.join() cause the join() to throw an InterruptedException immediately?

在 Thread.join() 之前调用 Thread.interrupt() 是否会导致 join() 立即抛出 InterruptedException?

No it will not throw. Only if the currentthread that is calling the join()method gets interrupted will join()throw InterruptedException. t.interrupt()is interrupting the thread you just started, while t.join()will only throw InterruptedExceptionif the thread that is doing the join-ing (maybe the main thread?) is itself interrupted.

不,它不会抛出。仅当调用该方法的当前线程join()被中断时才会join()抛出InterruptedExceptiont.interrupt()正在中断您刚刚启动的线程,而t.join()只有InterruptedException在进行连接的线程(可能是主线程?)本身被中断时才会抛出。

 Thread t = new Thread(someRunnable);
 t.start();
 t.interrupt();
 t.join();  // will _not_ throw unless this thread calling join gets interrupted

Also it is important to realize that interrupting a thread does not cancel it andjoin()is not like a Futurein that it will return the exception the thread threw.

同样重要的是要意识到中断线程不会取消它,并且join()不像 aFuture那样会返回线程抛出的异常。

When you interrupt a thread, any calls the thread is making to sleep(), wait(), join(), and other interruptible methods will throw InterruptedException. If those methods are not called then the thread will continue running. If a thread doesthrow a InterruptedExceptionin response to being interrupted and then quits, that exception will be lost unless you you used t.setDefaultUncaughtExceptionHandler(handler).

当您中断一个线程,任何调用线程正在为sleep()wait()join(),和其他中断的方法将抛出InterruptedException。如果未调用这些方法,则线程将继续运行。如果线程确实抛出 aInterruptedException以响应被中断然后退出,除非您使用t.setDefaultUncaughtExceptionHandler(handler).

In your case, if the thread is interrupted and finishes because it returns, then the join will finish -- it will not throw an exception. Common thread code to handle an interrupt properly is as follows:

在您的情况下,如果线程因返回而被中断并完成,则连接将完成——它不会抛出异常。正确处理中断的常见线程代码如下:

 public void run() {
    try {
       Thread.sleep(10000);
    } catch (InterruptedException e) {
       // a good pattern is to re-interrupt the thread when you catch
       Thread.currentThread().interrupt();
       // another good pattern is to make sure that if you _are_ interrupted,
       // that you stop the thread
       return;
    }
 }

回答by Peter Lawrey

interrupt()interrupts the thread you interrupted, not the thread doing the interrupting.

interrupt()中断您中断的线程,而不是执行中断的线程。

c.f.

比照

Thread.currentThread().interrupt();
t.join(); // will throw InterruptedException