Rashmi
Rashmi

Reputation: 281

executor Service multithreading

I have used ExecutorService executor = Executors.newFixedThreadPool(10) in my application. This is a static variable.

The corePoolsize is 10 threads. Suppose all the 10 thread is currently running with a task assigned. I would like to know what will happen if I create one more task and try to run it.

I tried in my system but what I see is the task is running without any problem.

Upvotes: 0

Views: 104

Answers (4)

AnirbanDebnath
AnirbanDebnath

Reputation: 1043

Your system normally has multiple processors. That's why you are unable to see any difference.

Even if you mention the thread pool as 10, the threads are actually being evenly distributed to all the processors in the system by the JVM. Thus the queue that is used to store the upcoming tasks are never full with just 10 threads.

I guess; even with Integer.MAX_VALUE number of tasks the waiting queue will not be fully loaded, as multiple processors would take up the tasks and divide among themselves.

Upvotes: -1

PyThon
PyThon

Reputation: 1067

Javadoc

At any point, at most "nThreads" threads will be active processing tasks.

May be any of your previously executing task got terminated due to some exception.

Upvotes: -1

Tunaki
Tunaki

Reputation: 137064

When you submit a new task when all the threads are currently active, it will be added to a queue in a FIFO manner and will be processed when one thread becomes available. This queue is unbounded so the maximum elements it can hold is Integer.MAX_VALUE.

This is answered in the Javadoc for Executors.newFixedThreadPool:

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.

Upvotes: 2

tddmonkey
tddmonkey

Reputation: 21184

Taken directly from the Javadoc

If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available

Upvotes: 1

Related Questions