美文网首页
JAVA中断正在运行线程的方法

JAVA中断正在运行线程的方法

作者: 放牛的星星 | 来源:发表于2018-03-07 21:31 被阅读44次

    Java或者Android开发中经常要中断线程,这里总结下中断正在运行的线程

    方法一:

    设置标记位置flag

    public class InterruptThreadTest {
        public static void main(String[] args) {
            InterruptThread t=new InterruptThread();
            t.start();
            try {
                Thread.sleep(5000);
                t.flag=false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
        static class InterruptThread extends Thread{
           public boolean flag=true;
            @Override
            public void run() {
                super.run();
                while (flag){
                    System.out.println("run.....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    方法二:

    使用java提供的中断的方法interrupt()

    /**
     * <描述>
     *
     * @author levi
     * @email lilinwei@xiaoyouzi.com
     * @date: 2018/3/7
     * @version: v1.0
     */
    public class InterruptThreadTest2 {
        public static void main(String[] args) {
            InterruptThread t=new InterruptThread();
            t.start();
            try {
                Thread.sleep(5000);
                t.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
    
        }
        static class InterruptThread extends Thread{
            @Override
            public void run() {
                super.run();
                while (true){
                    System.out.println("run.....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("InterruptedException.....");
                        e.printStackTrace();
                        break;//线程异常中断线程
                    }
                }
            }
        }
    }
    

    方法三:利用守护线程

    既在创建线程中创建一个守护线程,如果创建线程结束则守护线程也。方法三的使用场景模式,如:一个线程正在拷贝一个很大的文件或者正在网络请求,结束而且耗时超出预期,这时候又想中断它。如果使用方法一&方法二已经不起使用了。这时候就可以使用方法三:

    /**
     * <描述>
     *
     * @author levi
     * @email lilinwei@xiaoyouzi.com
     * @date: 2018/3/7
     * @version: v1.0
     */
    public class InterruptThreadTest3 {
        public static void main(String[] args) {
            InterruptThreadTest3 InterruptThread=new InterruptThreadTest3();
            long startTime=System.currentTimeMillis();
            InterruptThread.execute(()->{
                try {
                    System.out.println("在操作一个很大很大的文件....");
                    Thread.sleep(50_000);
                } catch (InterruptedException e) {
                    System.out.println("守护线程结束....");
                    e.printStackTrace();
                }
    
            });
            InterruptThread.shutDown(5_000);
            long endTime=System.currentTimeMillis();
            //在统计线程工作时间时,可能会比实际使用的时间长一些,因为在new,start的过程中消耗了一些时间
            System.out.println("工作了...>"+(endTime-startTime));
        }
        public Thread serviceThread;
        public boolean finished=false;
        public void execute(Runnable task){
            serviceThread=new Thread(()->{
                Thread wokerTask=new Thread(task);
                wokerTask.setDaemon(true);
                wokerTask.start();
                try {
                    wokerTask.join();//此线程等待子线程执行完时才退出
                    finished=true;
                } catch (InterruptedException e) {
                    System.out.println("创建线程中断(结束)");
    
                }
            });
            serviceThread.start();
    
        }
        //设置超时时间
        public void shutDown(long minlls){
            long currentTime=System.currentTimeMillis();
            while (!finished){//标记线程还没结束
                if(System.currentTimeMillis()-currentTime>minlls){
                    //超时了结束线程
                    serviceThread.interrupt();
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    break;//中断当前监控线程
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:JAVA中断正在运行线程的方法

          本文链接:https://www.haomeiwen.com/subject/jrwfzttx.html