shutdown and awaitTermination which first call have any difference?

前端 未结 9 1205
渐次进展
渐次进展 2020-12-12 14:44

What is the difference between

ExecutorService eService = Executors.newFixedThreadPool(2);
eService.execute(new TestThread6());
eService.execute(new TestThre         


        
9条回答
  •  半阙折子戏
    2020-12-12 15:02

    executorService.execute(runnableTask);  
    
    //executorService.shutdown(); //it will make the executorService stop accepting new tasks
    //executorService.shutdownNow(); //tires to destroy the executorService immediately, but it doesn't guarantee that all the running threads will be destroyed at the same time. This method returns list of tasks which are waiting to be processed.
    //List notExecutedTasks = executorService.shutdownNow(); //this method returns list of tasks which are waiting to be processed.developer decide what to do with theses tasks?
    
    //one good way to shutdown the executorService is use both of these methods combined with the awaitTermination
    executorService.shutdown();
    try{
        if(!executorService.awaitTermination(1000, TimeUnit.MICROSECONDS)) {
            executorService.shutdownNow();
        }
    }catch (InterruptedException e){
        e.printStackTrace();
    }
    

提交回复
热议问题