user2997351
user2997351

Reputation: 11

Java newFixedThreadpool keeps the first n threads suspended

I'm using Executors.newFixedThreadPool(N) where N is any number representing a pool capacity.

The problem is the following: Say we have 100 threads and N=10. All the Runnables are propagated to the pool through submit() method (actually execute method also behaves problematically).. After all the code in Runnable's run() is finished (the last line is executed) the pool still keeps the first N threads suspended, while other 90 are successfully finished. So varying the N value leads to corresponding change of suspended threads number. Here is the example of paused thread's call stack:

Unsafe.park(boolean, long) line: not available [native method] [local variables unavailable]    
LockSupport.park(Object) line: 156  
AbstractQueuedSynchronizer$ConditionObject.await() line: 1987   
LinkedBlockingQueue<E>.take() line: 399 
ThreadPoolExecutor.getTask() line: 947  
ThreadPoolExecutor$Worker.run() line: 907   
Thread.run() line: 662  

All the Runnables run the identical code and do not use shared resources..

Any ideas?

Upvotes: 0

Views: 214

Answers (1)

vandale
vandale

Reputation: 3650

The purpose of the thread pool is to have threads waiting to execute tasks so as to not incur the overhead of creating new threads when tasks must be done concurrently. When you create a thread pool via Executors.newFixedThreadPool(n), it will have up to n threads waiting to execute those tasks. One they've completed their task, they will wait until a new task is submitted, and must be explicity terminated. From the doc:

The threads in the pool will exist until it is explicitly shutdown.

Upvotes: 1

Related Questions