您的位置:首页 > 编程语言 > Java开发

Java线程中断interrupt interrupted isInterrupted

2019-09-17 15:14 99 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/jtsyh2008/article/details/100925031

interrupt

对thread对象设置中断标记,但不会执行中断操作。没有返回值。

interrupted & isInterrupted

简单看一下源码,interrupted()方法中调用了isInterrupted()

public static boolean interrupted() {
return currentThread().isInterrupted(true);
}

那么再看isInterrupted,不带参数的isInterrupted()调用了带参数的isInterrupted(false),
(带参数的isInterrupted方法是个native方法,所以就不深究了),看注释可以知道:这个boolean类型的参数ClearInterrupted就是是否reset中断标记的。

也就是说,interrupted()会重置中断标记,而isInterrupted()不会重置。
另一个区别是,interrupted()是一个static方法,用在当前线程;而isInterrupted()是一个instance方法,用在线程实例对象。

public boolean isInterrupted() {
return isInterrupted(false);
}
/**
* Tests if some Thread has been interrupted.  The interrupted state
* is reset or not based on the value of ClearInterrupted that is
* passed.
*/
private native boolean isInterrupted(boolean ClearInterrupted);

总结一下
interrupted(): static方法,检查当前线程的中断标记(返回值类型为boolean),并且会重置中断标记为false
isInterrupted(): 实例方法,检查实例线程的中断标记(返回值类型同样为boolean),但不会重置中断标记

Demo

Demo 1
只在main中调用t.interrupt(), 设置中断标记,但它本身不执行中断,所以输出
Entering thread
Running in C
Running in C
Running in C

直到生命尽头

public class GeneralInterrupt implements Runnable {
@Override
public void run() {
System.out.println("Entering thread");
while(true){
System.out.println("Running in C");
}
}

public static void main(String[] args) {
GeneralInterrupt si = new GeneralInterrupt();
Thread t = new Thread(si);
t.start();
try{
Thread.sleep(1000);
}catch (InterruptedException x){
System.out.println("interrupted");
}
t.interrupt();
}
}

Demo 2
main函数没有变化,run中使用中断标记控制循环。
对于第一个循环来说,当中断发生时,Thread.interrupted()为true,跳出第一个循环,同时Thread.interrupted()会将中断标记置为false,这样进入第二个循环Thread.currentThread().isInterrupted()值为false,然后无限循环。
输出
Entering thread
Running in C1
Running in C1


Running in C1
Running in C2
Running in C2

public class GeneralInterrupt implements Runnable {
@SuppressWarnings("static-access")
@Override
public void run() {
System.out.println("Entering thread");
//		while(true){
//			System.out.println("Running in C");
//		}
while(!(Thread.interrupted())){
//while(!(Thread.currentThread().isInterrupted())){
System.out.println("Running in C1");
}
while(!Thread.currentThread().isInterrupted()){
System.out.println("Running in C2");
}
System.out.println("Leaving thread");
}
//main没变
public static void main(String[] args) {
GeneralInterrupt si = new GeneralInterrupt();
Thread t = new Thread(si);
t.start();
try{
Thread.sleep(1000);
}catch (InterruptedException x){
System.out.println("interrupted");
}
t.interrupt();
}
}

Demo 3:
对Demo2来说只是把第一个循环中检查中断的语句换成Thread.currentThread().isInterrupted(),这意味着当中断发生时,它不会重置中断标记,所以当下一个循环检查中断标记仍然是false,就不会进入第二个循环。
输出
Entering thread
Running in C1
Running in C1


Leaving thread

public class GeneralInterrupt implements Runnable {
@SuppressWarnings("static-access")
@Override
public void run() {
System.out.println("Entering thread");
//		while(true){
//			System.out.println("Running in C");
//		}
//while(!(Thread.interrupted())){
while(!(Thread.currentThread().isInterrupted())){
System.out.println("Running in C1");
}
while(!Thread.currentThread().isInterrupted()){
System.out.println("Running in C2");
}
System.out.println("Leaving thread");
}

public static void main(String[] args) {
GeneralInterrupt si = new GeneralInterrupt();
Thread t = new Thread(si);
t.start();
try{
Thread.sleep(1000);
}catch (InterruptedException x){
System.out.println("interrupted");
}
t.interrupt();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: