多线程中的Interrupt、Interrupted、IsInterrupted 的区分

1. Interrupt是指对该线程设置了终止状态, 并没有终止该线程。


2. Interrupted是指判断当前线程是否终止了, 并且会由于interrupt设置的线程终止状态。

public class MyThread extends Thread {
	private int count;
	public void run() {
		for (int i = 0; i < 100000; i++) {
			count ++;
		}
	}
}

public class TestMain {
	public static void main(String[] args) throws InterruptedException {
		MyThread mt = new MyThread();
		mt.start();
		mt.interrupt();
		System.out.println(mt.interrupted()); // 当前线程是否中断状态  false
		System.out.println(mt.interrupted());// 当前线程是否中断状态  false
		

	}
}
public class TestInterrupted {
	public static void main(String[] args) {
		Thread.currentThread().interrupt();  // 此时设置为中断状态
		System.out.println(Thread.interrupted()); // 此时中断状态为True 但是 此方法清除了中断状态
		System.out.println(Thread.interrupted()); // 此时中断状态为False
	}
}



3. IsInterruped是指判断线程Thread对象是否已经是终止状态,但并不清除状态。


public class IsInterrupted {
	public static void main(String[] args) {
		MyThread mt = new MyThread();
		mt.start();
		mt.interrupt();
		System.out.println(mt.isInterrupted()); // 判断测试线程是否处于中断状态  并且不清除状态标识
		System.out.println(mt.isInterrupted()); // 判断测试线程Thread对象是否已经是中断状态 但不清除状态标识.
	}
}

没懂的朋友联系我 wangyan199366 (wechat)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值