JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

「Java」判断线程是否为停止状态

wys521 2024-11-18 18:01:07 精选教程 24 ℃ 0 评论

Thread.java类里提供了两种方法

(1)this.interrupted():测试当前线程是否已经中断。

(2)this.isInterrupted():测试线程是否已经中断。

interrupted()方法声明如下;

public static boolean interrupted()

isInterrupted()方法声明如下:

public boolean isInterrupted()

isInterrupted()方法是一个非静态的返回值为boolean的方法。isInterrupted 是作用于调用该方法的线程对象所对应的线程。(线程对象对应的线程不一定是当前运行的线程。例如我们可以在当前线程中去调用另一个线程对象的isInterrupted方法。)

interrupt()

此操作会将线程的中断标志位置位,至于线程作何动作那要看线程了。

如果线程sleep()、wait()、join()等处于阻塞状态,那么线程会定时检查中断状态位如果发现中断状态位为true,则会在这些阻塞方法调用处抛出InterruptedException异常,并且在抛出异常后立即将线程的中断状态位清除,即重新设置为false。抛出异常是为了线程从阻塞状态醒过来,并在结束线程前让程序员有足够的时间来处理中断请求。

如果线程正在运行、争用synchronized、lock()等,那么是不可中断的,他们会忽略。

可以通过以下三种方式来判断中断:

1)isInterrupted()

此方法只会读取线程的中断标志位,并不会重置。

2)interrupted()

此方法读取线程的中断标志位,并会重置。

3)throw InterruptException

抛出该异常的同时,会重置中断标志位。


我们先来看看this.interrupted()方法的解释:测试当前线程是否已经中断,当前线程是指运行this.interrupted方法的线程。我们代码来试验一下:

class MyThread extends Thread{
	@Override
    public void run() {
		 super.run();
		  for(int i=0;i<50000;i++) {
			  System.out.println("i="+(i+1));
		  }
	 }
}
public class Run {
	public static void main(String[] args) {
		try {
			MyThread thread=new MyThread();
			thread.start();
			Thread.sleep(1000);
			thread.interrupt();//停止thread对象所代表的对象
			System.out.println("是否停止1?="+thread.interrupted());//判断thread对象所代表的线程是否停止
			System.out.println("是否停止2?="+thread.interrupted());//判断thread对象所代表的线程是否停止
		}catch(InterruptedException e) {
			System.out.println("main catch");
			e.printStackTrace();
		}
		System.out.println("end!");
	}
}

部分输出结果为;

i=49999
i=50000
是否停止1?=false
是否停止2?=false
end!

可以看出线程并未停止,这就证明了interrupted()的解释:测试当前线程是否已经中断。这个当前线程是main,他从未中断过。

那么再看下面的代码;

class MyThread extends Thread{
	@Override
    public void run() {
		super.run();
		for(int i=0;i<500000;i++) {
			System.out.println("i="+(i+1));
		}
	}
}
public class Run {
	public static void main(String[] args) {
		Thread.currentThread().interrupt();//停止thread对象所代表的对象
		System.out.println("是否停止1?="+Thread.interrupted());//判断thread对象所代表的线程是否停止
		System.out.println("是否停止2?="+Thread.interrupted());//判断thread对象所代表的线程是否停止
		System.out.println("end!");
	}
}


输出结果为:

是否停止1?=true
是否停止2?=false
end!

从上述结果来看,方法interrupted()的确判断出当前线程是否是停止状态。但是为啥第二个布尔值是false?查看官方文档中对interrupted()方法的解释;

测试当前线程是否已经中断。线程的中断状态由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回false(第一次调用已清除了其中断状态后,且在第二次调用检验完中断状态前,当前线程再次中断的情况除外。)

所以interrupted方法具有清除状态的功能,所以第二次调用后返回的值为false。

下面再来介绍isInterrupted()方法。

class MyThread extends Thread{
	@Override
    public void run() {
		  super.run();
		  for(int i=0;i<55000;i++) {
			   System.out.println("i="+(i+1));
		  }
	}
}
public class Run {
	public static void main(String[] args) {
		try {
			MyThread thread=new MyThread();
			thread.start();
			Thread.sleep(1000);
			thread.interrupt();
			System.out.println("是否停止1?="+thread.isInterrupted());//判断thread对象所代表的线程是否停止
			System.out.println("是否停止2?="+thread.isInterrupted());//判断thread对象所代表的线程是否停止
		}
		catch(InterruptedException e) {
			System.out.println("mian catch");
			e.printStackTrace();
		}
		System.out.println("end!");
	}
}


部分输出结果为;

i=49999
i=50000
是否停止1?=false
是否停止2?=false
end!

可以看出,此方法并未清除状态标志。

在最后再来看下这两个方法的解释:

(1)this.interrupted():测试当前线程是否已经是中断状态,执行后具有将状态标志清楚为false的功能。

(2)this.isInterrupted():测试当前线程Thread对象是否已经是中断状态,但不清除状态标志。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表