前言本章分为两个主题如何正确关闭线程池shutdown和shutdownNow的区别1.线程池示例publicclassShutDownThreadPoolDemo{privateExecutorServiceservice=Executors.newFixedThreadPool(10);publicstaticvoidmain(String[]args){newShutDownThreadPoolDemo().executeTask();}publicvoidexecuteTask(){for(inti=0;i<100;i++){service.submit(()->{System.out.println(Thread.currentThread().getName()+"->执行");});}}}执行结果pool-1-thread-2->executepool-1-thread-3->executepool-1-thread-1->executepool-1-thread-4->executepool-1-thread-5->executepool-1-thread-6->execute...执行完成后,主线程会一直阻塞,那么如何关闭线程池呢?本章介绍ThreadPoolExecutor中涉及关闭线程池的5个方法,如下:,线程池在调用shutdown()方法后并没有立即关闭,因为这个线程池中可能还有很多任务正在执行,也可能任务队列中有大量任务等待执行。调用shutdown()方法后,线程池会执行完正在执行的任务和队列中等待的任务。如果shutdown()方法完全关闭后有新的任务提交,线程池会根据拒绝策略直接拒绝后续新提交的任务。本源码位置(jdk1.8版本)intc=ctl.get();//线程池中的线程数小于核心线程数if(workerCountOf(c)
