Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何使运行在另一个ExecutorService中的ExecutorService在外部ExecutorService上调用shutdown时不关闭?_Java_Executorservice - Fatal编程技术网

Java-如何使运行在另一个ExecutorService中的ExecutorService在外部ExecutorService上调用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

我有一个Executor服务在另一个Executor服务中运行,用于发送电子邮件。如果我在外部执行器上调用shutdown,它将等待内部执行器服务关闭,这将严重影响响应时间

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

您是对的。我的假设是错误的。应该是其他原因造成了延误。谢谢你的时间!