Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Executor Service Thread Pool [closed]

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?

like image 701
Izza Avatar asked Nov 29 '22 17:11

Izza


1 Answers

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.

like image 127
Sahil Muthoo Avatar answered Dec 06 '22 11:12

Sahil Muthoo