Pavani
Pavani

Reputation: 31

No threads left running after using ThreadPoolExecutor in Java

Pool size=8, active threads=0, queued tasks=0, completed tasks=5678. Why active threads are 0 when thread pool is active. Could some one explain me this. It suppose to be more than 0 right?

Upvotes: 0

Views: 601

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 340070

tl;dr

I suspect you set your core pool size to zero, or you said to let core threads time-out.

allowCoreThreadTimeOut( true )

To quote from the Keep-alive times section of the ThreadPoolExecutor Javadoc:

By default, the keep-alive policy applies only when there are more than corePoolSize threads, but method allowCoreThreadTimeOut(boolean) can be used to apply this time-out policy to core threads as well, so long as the keepAliveTime value is non-zero.

Pass true or false to that method to:

[set] the policy governing whether core threads may time out and terminate if no tasks arrive within the keep-alive time, being replaced if needed when new tasks arrive.

setCorePoolSize( 0 )

If you set the core pool size to zero, all threads will eventually time-out. In this case, setting allowCoreTheadTimeOut is irrelevant.

See the Reclamation section of that Javadoc page:

You can configure a pool to allow all unused threads to eventually die by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

Upvotes: 2

Related Questions