简体   繁体   English

Executor FixedThreadPool 执行后会发生什么,因为 executor 没有关闭方法?

[英]What happens to the Executor FixedThreadPool after execution, as executor does not have a shutdown method?

if I instantiate an Executor like so:如果我像这样实例化一个 Executor:

private final Executor executor = Executors.newFixedThreadPool(2);

And then execute some code as below:然后执行如下代码:

executor.execute(() -> {
                    doSomeThingHere();
                });

What happens to the threadpool does it get destroyed after the code executes?代码执行后线程池会被破坏吗? Executor does not have any shutdown method? Executor 没有任何关闭方法? I am experiencing a build up of threads in my application every time I run through the process of the app.每次我运行应用程序的过程时,我都会在我的应用程序中体验到线程的建立。 This may not be the cause but I want to understand this and rule it out.这可能不是原因,但我想了解这一点并排除它。 At the moment every time I want to run code on the background I instantiate a new executor in this way.目前,每次我想在后台运行代码时,我都会以这种方式实例化一个新的执行器。

Use ExecutorService instead of Executor since newFixedThreadPool() returns an ExecutorService and that has a shutdown method.使用ExecutorService而不是Executor因为newFixedThreadPool()返回一个ExecutorService并且有一个shutdown方法。


If the executor is created with a ThreadFactory that creates daemon threads (see setDaemon ), and there are no other non-daemon threads running, like the main thread or the EDT, the virtual machine will be terminated (stopping all threads)如果执行器是使用创建守护线程的ThreadFactory创建的(请参阅setDaemon ),并且没有其他非守护线程在运行,例如主线程或 EDT,则虚拟机将被终止(停止所有线程)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM