Java Executor Service Thread Pool [closed]

吃可爱长大的小学妹 提交于 2019-12-09 06:38:19

问题


If I create a fixed size thread pool with 10 threads in java using Executor framework:

private final ExecutorService pool;
pool = Executors.newFixedThreadPool(10);

and then try to submit more than 10 tasks (say for an example, 12 tasks);

for (int i = 0 ; i < 12 ; i++) {
    pool.execute(new Handler(myRunnable));
}

What will happen to the extra tasks (extra two tasks as per the example of 12 tasks)? Will they be blocked until a thread has finished its work?


回答1:


Quoting the Javadoc for Executors.newFixedThreadPool(int nThreads) (emphasis mine):

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

The Javadocs for code as mature as the Java Concurrency Framework contain a great wealth of knowledge. Keep them close.



来源:https://stackoverflow.com/questions/7274371/java-executor-service-thread-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!