ForkJoinPool creates a huge amount of workers

倾然丶 夕夏残阳落幕 提交于 2021-01-28 21:57:11

问题


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
        }
    }
}

回答1:


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.




回答2:


You are right about the huge number of worker threads. I wrote this in 2011 and it is still applicable today. The framework cannot do a proper join() so it either creates new worker threads or stalls.



来源:https://stackoverflow.com/questions/36422654/forkjoinpool-creates-a-huge-amount-of-workers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!