ThreadPoolExecutor.shutdown()和shutdownNow()

shutDown() 

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

            shutdownNow() 

     根据JDK文档描述,大致意思是:执行该方法,线程池的状态立刻变成STOP状态,并试图停止所有正在执行的线程,不再处理还在池队列中等待的任务,当然,它会返回那些未执行的任务

 

 

 

ExecutorService mapService

 

try {

          mapService.shutdown(); // Instructs queue to drain.

 

          // Wait for tasks to finish; do not use a time-based timeout.

          // (See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6179024)

          LOG.info("Waiting for map tasks");

          mapService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

        } catch (InterruptedException ie) {

          // Cancel all threads.

          mapService.shutdownNow();

          throw ie;

 

        }

 

 

参考:

JAVA线程池shutdown和shutdownNow的区别

 

你可能感兴趣的:(ThreadPoolExecutor.shutdown()和shutdownNow())