Interrupt()详解

Interrupt()详解

1.Interrupt()设置中断标志位为中断。

   1.1如果此时线程要进入阻塞(入执行sleep()),那么将发生中断异常

   1.2如果此时线程正在阻塞中(正在sleep()中),那么将发生中断异常

除了以上两种情况,线程将不会中断,而是继续正常执行

此时可以利用Thread.interrupted()判断是否中断而离开run()

Runnable1.java

package threed;


import java.util.concurrent.TimeUnit;


public class Runnable1 implements Runnable {


	static int id = 0;


	boolean runned = false;


	boolean sleeped = false;


	private volatile double b;


	public Runnable1() {
		id++;
	}


	@Override
	public void run() {
		System.out.println(this + " thread run");


		synchronized (this) {
			runned = true;
		}


		for (int i = 1; i < 10; i++) {
			b += (Math.PI + Math.E) / (double) i;
			Thread.yield();
		}
		System.out.println(this + " thread will sleeping");
		try {
			synchronized (this) {
				sleeped = true;
			}
			TimeUnit.SECONDS.sleep(50);
			System.out.println(this + " thread awaik");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


	public synchronized boolean getSleeped() {
		return sleeped;
	}


	public synchronized boolean getRunned() {
		return runned;
	}


	public String toString() {
		return "id:" + id;
	}


}

TestThread1.java

package threed;

public class TestThread1 {

	public static void main(String[] args) {
		Runnable1 target = new Runnable1();
		Thread t = new Thread(target);
		t.start();
		while (true) {
			if (target.getRunned() && !target.getSleeped()) {
				System.out.println("main call interrupt");
				t.interrupt();
				System.out.println("main call interrupt end");
				break;
			}
		}
	}
}

执行后输入入下

id:1 thread run//线程1执行
main call interrupt//man线程调用线程1的interrupt开始
main call interrupt end//man线程调用线程1的interrupt结束
id:1 thread will sleeping//但是线程1仍然继续执行
java.lang.InterruptedException: sleep interrupted//线程1调用sleep时抛出异常
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Unknown Source)
at java.util.concurrent.TimeUnit.sleep(Unknown Source)
at threed.Runnable1.run(Runnable1.java:37)
at java.lang.Thread.run(Unknown Source)

说明:

1.调用interrupt,线程将不会中断,而是继续正常执行

2.线程已经被调用了interrupt,如果此时线程要进入阻塞(入执行sleep()),那么将发生中断异常


将上面的TestThread1.java修改如下

package threed;

public class TestThread1 {

	public static void main(String[] args) {
		Runnable1 target = new Runnable1();
		Thread t = new Thread(target);
		t.start();
		while (true) {
			if (target.getRunned() && target.getSleeped()) {
				System.out.println("main call interrupt");
				t.interrupt();
				System.out.println("main call interrupt end");
				break;
			}
		}
	}
}
执行后输出如下

id:1 thread run
id:1 thread will sleeping//已经进入睡眠
main call interrupt
main call interrupt end
java.lang.InterruptedException: sleep interrupted//main线程调用线程1的interrupt,线程1马上就抛出了异常
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Unknown Source)
at java.util.concurrent.TimeUnit.sleep(Unknown Source)
at threed.Runnable1.run(Runnable1.java:36)
at java.lang.Thread.run(Unknown Source)

说明:

1.调用interrupt,如果此时线程正在阻塞中(正在sleep()中),那么将发生中断异常


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值