shutdown、shutdownNow方法的理解

shutdown()

1、当线程池调用该方法时,线程池的状态则立刻变成SHUTDOWN状态。此时,则不能再往线程池中添加任何任务,否则将会抛出RejectedExecutionException异常。但是,此时线程池不会立刻退出,直到添加到线程池中的任务都已经处理完成,才会退出。

shutdownNow()

1、根据JDK文档描述,大致意思是:执行该方法,线程池的状态立刻变成STOP状态,并试图停止所有正在执行的线程,不再处理还在池队列中等待的任务,当然,它会返回那些未执行的任务。
2、它试图终止线程的方法是通过调用Thread.interrupt()方法来实现的,但是大家知道,这种方法的作用有限,如果线程中没有sleep 、wait、Condition、定时锁等应用, interrupt()方法是无法中断当前的线程的。所以,ShutdownNow()并不代表线程池就一定立即就能退出,它可能必须要等待所有正在执行的任务都执行完成了才能退出。

没有阻塞方法的时候

          public class InterrupteTest extends Thread {
        
            public void run() {
               System.out.println("test");
               System.out.println("test--------");
            }
        
            public static void main(String[] args) throws InterruptedException {
                InterrupteTest t = new InterrupteTest();
                t.start();
                t.interrupt();
            }
        }

1、输出结果
test
test--------

再有阻塞方法的情况

    public class InterrupteTest extends Thread {
    
        @Override
        public void run() {
    
            try {
                sleep(10000);
                System.out.println("test");
            }catch (Exception e){
                System.out.println(" interrupted error");
            }
    
    
        }
    
        public static void main(String[] args) throws InterruptedException {
            InterrupteTest t = new InterrupteTest();
            t.start();
            t.interrupt();
        }
    }

1、输出结果
interrupted error

猜你喜欢

转载自blog.csdn.net/LiZhen314/article/details/126385604