2016-07-16 99 views
2

我正在实施一项服务,该服务执行一些较长时间运行的任务,此处由Thread.sleep(10000)模拟。当我按ctrl + c执行时,我从来没有得到我期望的InterruptedException。我想知道为什么。关闭ScheduledExecutorService安全吗?

package simple; 

import my.util.Now; 
import io.dropwizard.lifecycle.Managed; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.TimeUnit; 

public class EmailSender implements Managed { 

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailSender.class); 
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3); 

    // Constructor 
    public EmailSender() {} 

    @Override 
    public void start() { 
     LOGGER.info("Starting email sender thread: {}", Thread.currentThread().getName()); 
     Runnable task =() -> { 
      LOGGER.info("Running: {} in thread: {}", Now.now(), Thread.currentThread().getName()); 
      try { 
       Thread.sleep(10000); 
      } catch (InterruptedException ie) { 
       LOGGER.error("Task is being interrupted: {}", ie.getMessage()); 
      } 
     }; 
     executorService.scheduleAtFixedRate(task, 0, 5, TimeUnit.SECONDS); 
    } 

    @Override 
    public void stop() { 
     LOGGER.info("Stopping email sender thread: {}", Thread.currentThread().getName()); 
     executorService.shutdown(); 

    } 
} 

这样可以安全地假设executorService.shutdown();等到执行完成任务这样吗?

+2

该文档基本上说明了一切:['关机()'](https://docs.oracle.com/javase/7/ docs/api/java/util/concurrent/ExecutorService.html#shutdown())会阻止执行程序接受新的提交。您可能希望['shutdownNow()'](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#shutdownNow()),它会中断正在运行的线程。不,后者是不安全的,因为中断线程不是很安全。 – dhke

回答

2

shutdown不会终止正在运行的任务,它甚至会执行在队列中等待的任务。

如果您想立即终止,请使用shutdownNow

1

这是安全的假设executorService.shutdown();这样等待执行完成任务吗?

不会。它不会等待所有线程完成。从ExecutorService Oracle文档页面

推荐方式:

void shutdownAndAwaitTermination(ExecutorService pool) { 
    pool.shutdown(); // Disable new tasks from being submitted 
    try { 
    // Wait a while for existing tasks to terminate 
    if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { 
     pool.shutdownNow(); // Cancel currently executing tasks 
     // Wait a while for tasks to respond to being cancelled 
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) 
      System.err.println("Pool did not terminate"); 
    } 
    } catch (InterruptedException ie) { 
    // (Re-)Cancel if current thread also interrupted 
    pool.shutdownNow(); 
    // Preserve interrupt status 
    Thread.currentThread().interrupt(); 
    } 

shutdown():发起在以前已提交任务的执行一个有序的关闭,但没有新的任务将被接受。

shutdownNow():尝试停止所有正在执行的任务,停止等待任务的处理并返回正在等待执行的任务的列表。

您可以更改

if (!pool.awaitTermination(60, TimeUnit.SECONDS)) 

while (!pool.awaitTermination(60, TimeUnit.SECONDS)) 
Thread.sleep(60000);