线程既然又开始,那么就有结束,那我们如和来结束一个线程呢?
- 线程执行结束,自然终止。
- 线程内抛出未处理异常。
jdk早期有几个方法来结束或暂停线程
- stop() :会导致线程不会正确释放资源。
- resume() :恢复线程的执行,如果此方法出现在suspend()方法前,那么线程会一直处于挂起状态,并一直占用锁,而造成死锁,而且被挂起的线程状态还是Runnable;
- suspend() :暂停线程的执行,容易导致死锁,因为该线程在暂停的时候仍然占有该资源,不会释放资源,就会导致线程产生环路等待,造成死锁。
以上三个方法早已不推荐使用
接下来来看一下我们应该使用什么方式
interrupt()、isInterrupted()、interrupted();
- interrupt(): 中断一个线程,并不是强行关闭一个线程,类似发了个信号,线程会在一个合适的时机来中断自己;
- isInterrupted(): 判断当前线程是否处于中断状态;
- interrupted(): 判断当前线程是否处于中断状态,会把中断标志位改为false;
一、第一种方式
测试代码:
public class InterruptTest {
public static void main(String[] args) throws InterruptedException {
Thread newThread = new NewThread();
newThread.setName("interrupt");
newThread.start();
Thread.sleep(5L);//休眠5毫秒
newThread.interrupt();//发出中断信号
}
public static class NewThread extends Thread{
@Override
public void run(){
while (!isInterrupted()){//判断线程是否中断
//不中断
System.out.println(Thread.currentThread().getName()+"'s status is : "+isInterrupted());
}
//中断
System.out.println(Thread.currentThread().getName()+"'s status is : "+isInterrupted());
}
}
}
运行结果:
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : false
interrupt's status is : true
Process finished with exit code 0
二、当线程内抛出InterruptedException异常的时候
public class InterruptTest {
public static void main(String[] args) throws InterruptedException {
Thread newThread = new NewThread();
newThread.setName("interrupt");
newThread.start();
Thread.sleep(5L);
newThread.interrupt();
}
public static class NewThread extends Thread{
@Override
public void run(){
while (!isInterrupted()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName()+"'s exception is : "+isInterrupted());
}
System.out.println(Thread.currentThread().getName()+"'s while status is : "+isInterrupted());
}
System.out.println(Thread.currentThread().getName()+"'s status is : "+isInterrupted());
}
}
}
运行结果:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wangl.myTest.InterruptTest$NewThread.run(InterruptTest.java:23)
interrupt's exception is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
interrupt's while status is : false
.
.
.
.
大家会发现,在线程内抛出InterruptedException异常的时候,线程不会被中断,所以线程会一直运行,这是因为在线程抛出InterruptedException异常的时候,
线程的中断标志位会被复位成false,这时候我们应该在catch中再interrupt()一次。
当我们在catch中加上interrupt()后,运行结果:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wangl.myTest.InterruptTest$NewThread.run(InterruptTest.java:23)
interrupt's exception is : false
interrupt's while status is : true
interrupt's status is : true
Process finished with exit code 0