简体   繁体   English

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

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

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 interrupted status of the thread before doing its "wait" routine? 我猜测Thread.join()在执行“等待”例程之前检查线程的interrupted状态?

interrupt() interrupts the thread you interrupted, not the thread doing the interrupting. interrupt()中断你中断的线程,而不是中断执行的线程。

cf 比照

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

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 current thread that is calling the join() method gets interrupted will join() throw InterruptedException . 只有当调用join()方法的当前线程被中断时, join()才会抛出InterruptedException t.interrupt() is interrupting the thread you just started, while t.join() will only throw InterruptedException if the thread that is doing the join-ing (maybe the main thread?) is itself interrupted. t.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 and join() is not like a Future in that it will return the exception the thread threw. 此外,重要的是要意识到中断线程不会取消它, 并且 join()不像Future ,因为它将返回线程抛出的异常。

When you interrupt a thread, any calls the thread is making to sleep() , wait() , join() , and other interruptible methods will throw InterruptedException . 当你中断一个线程时,线程进入sleep()wait()join()和其他可中断方法的任何调用都将抛出InterruptedException If those methods are not called then the thread will continue running. 如果未调用这些方法,则线程将继续运行。 If a thread does throw a InterruptedException in response to being interrupted and then quits, that exception will be lost unless you you used t.setDefaultUncaughtExceptionHandler(handler) . 如果一个线程确实抛出了InterruptedException以响应被中断然后退出,那么除非你使用了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;
    }
 }

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

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