并发编程(3)——interrupt()中断线程

7 篇文章 0 订阅
1 篇文章 0 订阅

上一篇:并发编程(2)—— 创建线程的方式

线程既然又开始,那么就有结束,那我们如和来结束一个线程呢?

  1. 线程执行结束,自然终止。
  2. 线程内抛出未处理异常。

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

下一篇:并发编程(4)—— 线程的生命周期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值