Java中Thread类的interrupt(),interrupted()和isInterrupted()方法详解

先介绍一下这三个方法:

  • interrupt()方法

中断此线程(不一定是当前线程,指Thread实例代表的线程),该方法实际上是给线程设置一个中断状态,并没有实际中断线程,线程仍会继续执行。如果interrupt()不会中断sleep,wait,join方法,就不会抛InterruptException异常,就不会清除中断标志位isInterrupt()返回true

如果中断sleep,wait,join方法,就会抛InterruptException异常,就会清除中断标志位,isInterrupt()返回false。(参考第二个代码示例)

  • interrupted()方法

测试当前线程是否中断,该方法将清除线程的中断状态。也就是说,当连续调用两次该方法,第一次使用返回true,并清除中断状态,第二次调用时由于中断状态已经被清除,将返回false。

  • isInterrupted()方法

仅仅查询中断状态来判断是否发生中断并返回true或者false。

参考示例1:

class MyThread extends Thread{
	@Override
	public void run() {
		System.out.println("MyThread started!");
		for(int i=1;;i++) {
			System.out.println("loop i="+i);
			if(this.isInterrupted()) {//当前为中断状态
				System.out.println("检测到当前为中断状态!");
				System.out.println("第一次interrupted()"+this.interrupted());
				//清除中断状态
				System.out.println("第二次interrupted()"+this.interrupted());
				break;
			}
		}
		System.out.println("MyThread stopped!");
	}
}
public class TestInterrupt {
	public static void main(String[] args) {
		System.out.println("Main Thread started!");
		MyThread thread=new MyThread();
		thread.start();
		//中断线程
		thread.interrupt();
		try {
			Thread.currentThread().sleep(1000);//主线程等待两秒,等待MyThread线程运行结束
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("thread是否存活:"+thread.isAlive());
		System.out.println("Main Thread stopped!");
	}
}

运行结果如下:

Java中Thread类的interrupt(),interrupted()和isInterrupted()方法详解_第1张图片

对于中断sleep(),join(),wait()方法的情况,我们可以参考示例2:

import java.util.concurrent.TimeUnit;

class MyThread extends Thread{
	@Override
	public void run() {
		System.out.println("MyThread started!");
		for(int i=1;;i++) {
			System.out.println("loop i="+i);
			if(this.isInterrupted()) {//当前为中断状态
				System.out.println("检测到当前为中断状态!");
				break;
			}
			try {
				Thread.currentThread().sleep(500);
			} catch (InterruptedException e) {
				System.out.println("第一次中断之后的isInterrupted()返回值:"+Thread.currentThread().isInterrupted());
				System.out.println("-----------------再次中断线程-----------------");
				//只要中断join,wait,sleep就会清除中断状态,造成线程处于无限执行的状态。所以必须在这里再次中断
				Thread.currentThread().interrupt();
				System.out.println("再次中断之后的isInterrupted()返回值:"+Thread.currentThread().isInterrupted());
			}
		}
		System.out.println("MyThread stopped!");
	}
}
public class TestInterrupt {
	public static void main(String[] args) {
		System.out.println("Main Thread started!");
		MyThread thread=new MyThread();
		thread.start();
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//中断线程
		thread.interrupt();
		System.out.println("Main Thread stopped!");
	}
}

注意上面代码当中,在捕获InterruptedException的地方,再一次的进行中断线程操作,这是因为中断join(),wait(),sleep()方法时,interrupt()方法会将中断状态清除,如果在这个地方不进行第二次中断,则线程会一直执行下去,导致占用CPU资源。所以这里必须再次进行interrupt()方法。

最后总结,关于这三个方法,interrupt()是给线程设置中断状态并不实际中断线程执行;interrupted()是检测中断并清除中断状态;isInterrupted()只查询中断状态。还有重要的一点就是interrupted()作用于当前线程,interrupt()和isInterrupted()作用于实例所代表的线程。

你可能感兴趣的:(多线程,java,多线程,并发编程)