Is idle thread taking CPU execute time in Java Executors?

cpu-time, java, multithreading, threadpoolexecutor

Solution

No, these threads are created lazily, or 'on-demand'. As stated in the documentation (emphasis mine):

On-demand construction

By default, even core threads are initially created and started only when new tasks arrive

Java provides methods to override this default and allow for eager creation, namely `prestartCoreThread` and `prestartAllCoreThreads`.

Once threads are actually created, idle ones (generally) won't take CPU time as there is no reason for them to be scheduled on a core when they have no work to do.

They will still hold on to some memory however, for their stack and whatnot.

Problem

When I have this code in an application: ``` Executors.newFixedThreadPool(4); ``` but I never use this thread pool. Will the idle threads consume CPU time? If so - why?

Original source