在Thread.join()之前调用Thread.interrupt()会导致join()立即抛出一个InterruptedException? [英] Does calling Thread.interrupt() before a Thread.join() cause the join() to throw an InterruptedException immediately?

查看:1183
本文介绍了在Thread.join()之前调用Thread.interrupt()会导致join()立即抛出一个InterruptedException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Basically, what the question title says.

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

从我自己的测试,似乎,但只是想确定。我猜在 Thread.join()检查中断状态的线程之前做等待例程?

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?

推荐答案

interrupt()中断您中断的线程,做中断。

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

cf

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

这篇关于在Thread.join()之前调用Thread.interrupt()会导致join()立即抛出一个InterruptedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆