Search code examples
javaparallel-processingfork-joinforkjoinpool

ForkJoinPool creates a huge amount of workers


I use the ForkJoinPool to execute tasks in parallel. When I look at the logout put of my program it seems that the ForkJoinPool creates a huge amount of workers to execute my tasks (there are log entries that look like this: 05 Apr 2016 11:39:18,678 [ForkJoinPool-2-worker-2493] <message>).

Is there a worker for each tasks created which is then executed according to the number of parallelism I configured in the ForkJoinPool or am I doing something wrong? Here is how I do it:

public class MyClass {
    private static final int NUM_CORES = Runtime.getRuntime().availableProcessors();
    public MyClass() {
        int maxThreads = NUM_CORES * 2;
        this.forkJoinPool = new ForkJoinPool(maxThreads);
    }

    public void doStuff() {  
        final int[] toIndex = {0};
        forkJoinPool.submit(() -> {
            List<ForkJoinTask> tasks = new ArrayList<>();
            while (toIndex[0] < objects.size()) {
                toIndex[0] += 20;
                List<Object> bucket = objects.subList(toIndex[0] - 20, toIndex[0]);
                ForkJoinTask task = new UpdateAction(bucket);
                tasks.add(task);
                task.fork();
            }
            tasks.forEach(ForkJoinTask::join);
        }).join();
    }

    private class UpdateAction extends RecursiveAction {

        private List<Object> bucket;

        private UpdateAction(List<Object> bucket) {
            this.bucket = bucket;
        }

        @Override 
        protected void compute() {
            // do some calculation
        }
    }
}

Solution

  • The number at the end of a task name has nothing to do with the actual number of threads used by the pool. Take a look at the registerWorker method of the ForkJoinPool class. It looks something like this:

    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
        UncaughtExceptionHandler handler;
        wt.setDaemon(true);                           // configure thread
        if ((handler = ueh) != null)
            wt.setUncaughtExceptionHandler(handler);
        WorkQueue w = new WorkQueue(this, wt);
        int i = 0;                                    // assign a pool index
        int mode = config & MODE_MASK;
        int rs = lockRunState();
        ...
        // some manipulations with i counter
        ...
        wt.setName(workerNamePrefix.concat(Integer.toString(i >>> 1)));
        return w;
    }
    

    workerNamePrefix is initialised to

    "ForkJoinPool-" + nextPoolId() + "-worker-" 
    

    If you want to measure the real number of threads used by the pool you better log what getPoolSize() returns.