java

java 中优雅的停止线程

2019-05-25  本文已影响0人  spraysss

方法一 轮询一个volatile的boolean变量

package com.example.stopthread;

public class ThreadStop1 {
     public static class MyThread extends Thread{
         private volatile boolean start=true;
         @Override
         public void run() {
             while(start){
                 doTask();
             }

         }
         private void doTask(){
             System.out.println("do some work");
             try {
                 Thread.sleep(100);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
         private void shutDown(){
             start=false;
         }
     }

    public static void main(String[] args) {
        MyThread t1=new MyThread();
        t1.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.shutDown();
    }
}

方法二 线程中断

package com.example.stopthread;

public class ThreadStop2 {
    public static class MyThread extends Thread{
        @Override
        public void run() {
            while(true){
                try {
                    doTask();
                } catch (InterruptedException e) {
                    System.out.print("receive interrupt ");
                    break;
                }
            }

        }
        private void doTask() throws  InterruptedException{
            System.out.println("do some work");
            Thread.sleep(100);
        }
    }

    public static void main(String[] args) {
        MyThread t1=new MyThread();
        t1.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
}

超时停止

有时候线程在轮询的时候,由于内部执行了一个非常耗时并且阻塞的操作,以至于无法及时的收到外部的中断信号,这样就会出现及时已经对该线程发出了中断信号,但是线程也无法及时停止

这时候可以在执行线程之上再包一层,由外部线程处理器中断,内部线程设为守护线程,那么就可以做到及时响应和停止了

package com.example.stopthread;

public class ThreadStopForce {
    private Thread bootStrapThread;
    private volatile boolean finished=false;
    public void execute(final Runnable task){
        bootStrapThread=new Thread(){
            @Override
            public void run() {
                Thread worker =new Thread(task);
                worker.setDaemon(true);
                worker.start();
                try {
                    worker.join();
                    finished=true;
                } catch (InterruptedException e) {
                    //System.out.println("a");
                    //e.printStackTrace();
                }
            }
        };
        bootStrapThread.start();

    }
    private void shutDown(long time){
        long begin=System.currentTimeMillis();
        while(!finished){
            if(System.currentTimeMillis()-begin>time){
                System.out.println("time out ,showdown force");
                bootStrapThread.interrupt();
                break;
            }else {
                try {
                    bootStrapThread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        ThreadStopForce threadStopForce=new ThreadStopForce();
        threadStopForce.execute(new Runnable() {
            public void run() {
                //block and busy
                while (true){

                }
            }
        });
        threadStopForce.shutDown(10000);
    }
}

上一篇 下一篇

猜你喜欢

热点阅读