关闭线程

关闭线程

通过interruptionException关闭
import java.util.concurrent.TimeUnit;

/**
 * @author husky
 * @date 2019/7/11 11:23
 */
public class CloseThreadByInterruptException {
    public static void main(String[] args) {
        Thread workThread = new Thread(){
            @Override
            public void run() {
                System.out.println("i will start work");
                for(;;){
                    //working
                    //使用时间模拟工作
                    long start = System.currentTimeMillis();
                    long end = start + 1000 * 5;
                    while(System.currentTimeMillis() <= end){
                        //do nothing
                    }
                    try{
                        TimeUnit.MICROSECONDS.sleep(1);
                    }catch (InterruptedException e){
                        break;
                    }
                }
                System.out.println("i will be exiting");
            }
        };
        workThread.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("work thread will be closed");
        workThread.interrupt();
    }
}
通过interrupt status
import java.util.concurrent.TimeUnit;

/**
 * @author husky
 * @date 2019/7/11 11:16
 */
public class CloseThreadByInterruptStatus {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                while(!isInterrupted()){
                    //do something
                }
            }
        };
        t1.start();
        // ensure thread is running
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Thread will be closed");
        t1.interrupt();

    }
}
通过volatileinterrupt status关闭
import java.util.concurrent.TimeUnit;

/**
 * @author husky
 * @date 2019/7/11 11:39
 */
public class CloseThreadByVolatile {
    static class WorkThread extends Thread{
        private volatile boolean flag = false;
        @Override
        public void run() {
            System.out.println("i will start work");
            while(!flag&&!isInterrupted()){
                //正在执行
            }
            System.out.println("i will be exiting");
        }

        public void close(){
            this.flag = true;
            this.interrupt();
        }
    }

    public static void main(String[] args) {
        WorkThread workThread = new WorkThread();
        workThread.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("System will be interrupt");
        workThread.close();
    }
}

总结

interrupt status容易被清除,所以推荐使用voliate和interrupt status混合使用

该文学习于汪文君高并发编程

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值