Reputation: 67
If I create ExecutorService with thread pool size 10(using newFixedThreadPool) and initially only 5 tasks are running, in this case ExecutorService create 10 threads and 5 will be ideal or only 5 thread will be created.
Upvotes: 3
Views: 176
Reputation: 4259
If you use ExecutorService.newFixedThreadPool(nThreads)
then you will create a thread pool of size nThreads
. All threads will be created and will be idle if they have nothing to do.
ExecutorSerivce creates the constructor of ThreadPoolExecutor
with the parameters:
corePoolSize
is nThreads
maximumPoolSize
is nThreads
keepAliveTime
is 0
unit
is TimeUnit.MILLISECONDS
workQueue
is a "unbound" queue LinkedBlockingQueue
Integer.MAX_VALUE
, which normal programs should never reach)If you don't want that, you will have to create a ThreadPoolExecutor
yourself.
Note that you have to use a queue
with an upper-bound (that is actually reached when you run your software normally), else no new Threads
will be created - because that only happens if the queue is full.
Upvotes: 1
Reputation: 2670
In this case 10 thread will be created in thread pool. Then 5 thread will be assigned to task. and other 5 will be waiting for new task to come up.
Apart from this if any of 5 running thread is completed, it will be return to pool.
Upvotes: 0