Search code examples
javajava.util.concurrent

SingleThreadExecutor and ThreadFactory


Am I right that every time, when we performing several submit operations:

ExecutorService executor = Executors.newSingleThreadExecutor(
            new MyThreadFactory("someExecutor"));
executor.submit(...);
executor.submit(...);
executor.submit(...);

Method java.util.concurrent.ThreadFactory#newThread executes only once? Or it executes every time and creates a new thread per submit call?


Solution

  • It reuses the same thread everytime.

    That is the beauty of thread pools: avoid the cost of creating a new thread by reducing the per-task invocation overhead.

    You can read more about how threads are actually created in the ThreadPoolExecutor documentation.