你能解释一下调用java.lang.Thread.interrupt()时做什么吗?
当前回答
Thread.interrupt()方法设置内部的“中断状态”标志。通常该标志由Thread.interrupted()方法检查。
按照惯例,任何通过InterruptedException存在的方法都必须清除中断状态标志。
其他回答
中断是对线程的一个指示,它应该停止正在做的事情并做其他事情。由程序员决定线程如何响应中断,但是线程终止是很常见的。 一个很好的参考:https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
公共无效中断()
中断这个线程。
除非当前线程正在中断自身(这总是允许的),否则调用该线程的checkAccess方法,这可能导致抛出SecurityException。
如果这个线程在Object类的wait()、wait(long)或wait(long, int)方法的调用中被阻塞,或者该类的join()、join(long)、join(long, int)、sleep(long)或sleep(long, int)方法的调用中被阻塞,那么它的中断状态将被清除,它将接收一个InterruptedException。
如果这个线程在一个可中断通道上的I/O操作中被阻塞,那么通道将被关闭,线程的中断状态将被设置,并且线程将收到一个ClosedByInterruptException。
如果这个线程在选择器中被阻塞,那么线程的中断状态将被设置,并且它将立即从选择操作中返回,可能是一个非零值,就像调用选择器的唤醒方法一样。
如果以上条件都不满足,则该线程的中断状态将被设置。
中断一个不活跃的线程不会有任何影响。
抛出: 如果当前线程不能修改该线程,则返回SecurityException
在以上回答的基础上,我想补充一两件事。
需要记住的一件事是,调用中断方法并不总是会导致InterruptedException。因此,实现代码应该定期检查中断状态并采取适当的操作。 thread. currentthread (). isinterrupted()也可以用来检查线程的中断状态。与Thread.interrupted()方法不同,它不清除中断状态。
为了完整起见,除了其他答案外,如果线程在阻塞Object.wait(..)或thread .sleep(..)等之前被中断,这相当于在阻塞该方法时立即被中断,如下例所示。
public class InterruptTest {
public static void main(String[] args) {
Thread.currentThread().interrupt();
printInterrupted(1);
Object o = new Object();
try {
synchronized (o) {
printInterrupted(2);
System.out.printf("A Time %d\n", System.currentTimeMillis());
o.wait(100);
System.out.printf("B Time %d\n", System.currentTimeMillis());
}
} catch (InterruptedException ie) {
System.out.printf("WAS interrupted\n");
}
System.out.printf("C Time %d\n", System.currentTimeMillis());
printInterrupted(3);
Thread.currentThread().interrupt();
printInterrupted(4);
try {
System.out.printf("D Time %d\n", System.currentTimeMillis());
Thread.sleep(100);
System.out.printf("E Time %d\n", System.currentTimeMillis());
} catch (InterruptedException ie) {
System.out.printf("WAS interrupted\n");
}
System.out.printf("F Time %d\n", System.currentTimeMillis());
printInterrupted(5);
try {
System.out.printf("G Time %d\n", System.currentTimeMillis());
Thread.sleep(100);
System.out.printf("H Time %d\n", System.currentTimeMillis());
} catch (InterruptedException ie) {
System.out.printf("WAS interrupted\n");
}
System.out.printf("I Time %d\n", System.currentTimeMillis());
}
static void printInterrupted(int n) {
System.out.printf("(%d) Am I interrupted? %s\n", n,
Thread.currentThread().isInterrupted() ? "Yes" : "No");
}
}
输出:
$ javac InterruptTest.java
$ java -classpath "." InterruptTest
(1) Am I interrupted? Yes
(2) Am I interrupted? Yes
A Time 1399207408543
WAS interrupted
C Time 1399207408543
(3) Am I interrupted? No
(4) Am I interrupted? Yes
D Time 1399207408544
WAS interrupted
F Time 1399207408544
(5) Am I interrupted? No
G Time 1399207408545
H Time 1399207408668
I Time 1399207408669
含义:如果你像下面这样循环,并且中断发生在控制已经离开Thread.sleep(..)并且正在循环的确切时刻,异常仍然会发生。因此,依赖InterruptedException在线程被中断后可靠地抛出是完全安全的:
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
break;
}
}
什么是中断?
中断是一个指示 线程应该停止它是什么 做和做别的事情。由 由程序员来决定 线程如何响应中断, 但这对于线程来说是很常见的 终止。
它是如何实现的?
实现了中断机制 使用内部标志 中断状态。调用 interrupt设置这个标志。当 线程通过检查中断 调用静态方法 中断状态 清除。非静态的 线程。被使用的isInterrupted 一个线程来查询中断 另一种状态,并不会改变 中断状态标志。
引用自Thread.interrupt() API:
Interrupts this thread. First the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown. If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException. If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked. If none of the previous conditions hold then this thread's interrupt status will be set.
看看下面的内容,你就能完全理解:
http://download.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 自动化invokerrequired代码模式
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder