线程的Interrupted

本文详细解析了Java中线程中断的实现机制,包括如何使用t.interrupt()方法标记线程中断状态,以及线程在遇到wait、join或sleep等方法时如何响应中断并抛出InterruptedException。

线程调用t.interrupt();其实就是给线程设置一个中断状态的标识,并不能让线程马上终止。
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.  。

如果线程调用了wait ,jion, sleep方法而阻塞->>t.interrupt() -->将会抛出一个InterruptedException,异常抛出后会把刚才设置的中断状态标识也清除掉。

public class InterruptDemo {

	public static void main(String[] args) {

		Thread ex = new ExThread();
		ex.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		ex.interrupt();

	}

}

class ExThread extends Thread {

	@Override
	public void run() {
		super.run();
		while (!Thread.currentThread().isInterrupted()) {
			System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted());
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				System.out.println("InterruptedException异常:" + isInterrupted());//这里是false,因为异常抛出后,会清除中断状态的标识。所以While循环条件依旧满足,线程继续在那run..

			}

		}
	}

}
运行结果:

ExThread 运行中:false
ExThread 运行中:false
java.lang.InterruptedException: sleep interrupted
InterruptedException异常:false at java.lang.Thread.sleep(Native Method)
at com.thread.ExThread.run(InterruptDemo.java:28)
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false

如果我们在catch (InterruptedException e) 中再设置一下 this.interrupt();
public class InterruptDemo {

	public static void main(String[] args) {

		Thread ex = new ExThread();
		ex.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		ex.interrupt();

	}

}

class ExThread extends Thread {

	@Override
	public void run() {
		super.run();
		while (!Thread.currentThread().isInterrupted()) {
			System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted());
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				System.out.println("InterruptedException异常:" + isInterrupted());
				this.interrupt();
				System.out.println("是否中断:" + this.isInterrupted());

			}

		}
	}

}



运行结果如下:

ExThread 运行中:falseExThread 运行中:falseInterruptedException异常:false是否中断:truejava.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.thread.ExThread.run(InterruptDemo.java:28)

看吧,线程的whlie停止了。把上面的代码稍微改下:

public class InterruptDemo {

	public static void main(String[] args) {

		Thread ex = new ExThread();
		ex.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		ex.interrupt();

	}

}

class ExThread extends Thread {

	@Override
	public void run() {
		super.run();
		while (!Thread.currentThread().isInterrupted()) {
			System.out.println("ExThread 运行中:" + Thread.currentThread().isInterrupted());
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				System.out.println("InterruptedException异常:" + isInterrupted());
				this.interrupt();
<span style="color:#ff0000;">				System.out.println("是否中断:" + Thread.interrupted());
				System.out.println("isInterrupted:" + this.isInterrupted());</span>

			}

		}
	}

}


运行结果变成:

ExThread 运行中:false
ExThread 运行中:false
java.lang.InterruptedException: sleep interrupted
InterruptedException异常:false
是否中断:true
isInterrupted:false
ExThread 运行中:false
at java.lang.Thread.sleep(Native Method)
at com.thread.ExThread.run(InterruptDemo.java:28)
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false
ExThread 运行中:false


为嘛呢? 因为Thread.interrupted()如果当前线程isAlive()不仅会返回当前线程的的中断状态值,并且会清除它,就是说返回true,后把它改为false.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值