Java线程中断interrupt interrupted isInterrupted

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();
	}
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值