Interrupt中断线程

本文通过一个Java示例演示了如何使用Thread.sleep方法与中断标志来尝试停止一个正在运行的线程,并对比了两种不同的线程阻塞实现方式及其效果。

 

package com.wistron.swpc.ecs.util;

public class WrongWayStopThread extends Thread{

    public static void main(String[] args) {
        WrongWayStopThread thread = new WrongWayStopThread();
        System.out.println("Starting thread...");
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("Interrupting thread... ");
        thread.interrupt();
        
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("Stopping application...");
    }
    
    public void run(){
        while(!this.isInterrupted()){
            System.out.println("Thread is running...");
            //下面几行代码是模拟Thread.sleep(1000),至于为啥不用sleep是因为用了sleep无法正确中断线程
            long time = System.currentTimeMillis();
            while(System.currentTimeMillis()-time < 1000){
              //减少屏幕输出的空循环
            }
        }
    }

}

运行结果:

Starting thread...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread... 
Stopping application...

上面模拟的sleep的方法换成sleep

package com.wistron.swpc.ecs.util;

public class WrongWayStopThread extends Thread{

    public static void main(String[] args) {
        WrongWayStopThread thread = new WrongWayStopThread();
        System.out.println("Starting thread...");
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("Interrupting thread... ");
        thread.interrupt();
        
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("Stopping application...");
    }
    
    public void run(){
        while(!this.isInterrupted()){
            System.out.println("Thread is running...");
            //下面几行代码是模拟Thread.sleep(1000),至于为啥不用sleep是因为用了sleep无法正确中断线程
            /*long time = System.currentTimeMillis();
            while(System.currentTimeMillis()-time < 1000){
              //减少屏幕输出的空循环
            }*/
            
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

运行结果:

Starting thread...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread... 
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at com.wistron.swpc.ecs.util.WrongWayStopThread.run(WrongWayStopThread.java:39)
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Stopping application...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Thread is running...

换成sleep之后不能结束线程

 

转载于:https://www.cnblogs.com/tarrying/p/9071920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值