Java-如何使运行在另一个ExecutorService中的ExecutorService在外部ExecutorService上调用shutdown时不关闭?
我有一个Executor服务在另一个Executor服务中运行,用于发送电子邮件。如果我在外部执行器上调用shutdown,它将等待内部执行器服务关闭,这将严重影响响应时间Java-如何使运行在另一个ExecutorService中的ExecutorService在外部ExecutorService上调用shutdown时不关闭?,java,executorservice,Java,Executorservice,我有一个Executor服务在另一个Executor服务中运行,用于发送电子邮件。如果我在外部执行器上调用shutdown,它将等待内部执行器服务关闭,这将严重影响响应时间 private final ExecutorService blastExecutor = Executors.newFixedThreadPool(20); private final ExecutorService mailExecutor = Executors.newFixedThreadPool(2); pub
private final ExecutorService blastExecutor = Executors.newFixedThreadPool(20);
private final ExecutorService mailExecutor = Executors.newFixedThreadPool(2);
public void findDealers() {
blastExecutor.execute(new Runnable() {
public void run() {
try {
identifyDealers();
} catch (Exception e) {
}
}
});
blastExecutor.shutdown();
try {
blastExecutor.awaitTermination(30, TimeUnit.MINUTES);
} catch (InterruptedException e) {
}
logger.info("Completed sending request blasts.");
}
public void identifyDealers() {
mailExecutor.execute(new Runnable() {
public void run() {
try {
sendEmail();
} catch (Exception e) {
}
}
});
}
在这里,在blastExecutor.shutdown()
中,它也调用了mailExecutor
的shutdown(?)
这只是一个示例代码,在
identifiedalders()
method中发生了很多业务。我如何使sendEmail异步并使blastExecutor.shutdown()
不等待mailExecutor的关闭?您的假设似乎是错误的,shutdown()
应该只作用于外部执行器。它不会关闭任何其他执行人
下面是一个基于您的代码的小示例:
ExecutorService e1 = Executors.newFixedThreadPool(20);
final ExecutorService e2 = Executors.newFixedThreadPool(2);
e1.execute(new Runnable() {
public void run() {
System.out.println("e1 started");
e2.execute(new Runnable() {
public void run() {
System.out.println("e2 started");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("e2 completed");
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
});
e1.shutdown();
System.out.println("e1 shut down signal send");
e1.awaitTermination(30, TimeUnit.MINUTES);
System.out.println("e1 terminated");
e2.awaitTermination(30, TimeUnit.MINUTES);
System.out.println("e2 terminated");
应用程序不会终止,它会阻塞e2。等待终止
,并且在输出中还显示e2
从未收到关闭信号:
e1 started
e1 shut down signal send
e2 started
e1 terminated
e2 completed
如果mailExecutor
真的关闭了,我想还有一些事情没有显示在您的代码中。从代码的基本结构来看,您正在运行一个任务并立即关闭executer,这可能意味着您甚至不需要blastExecutor
您是对的。我的假设是错误的。应该是其他原因造成了延误。谢谢你的时间!