ForkJoinPool 创造了大量的工人 [英] ForkJoinPool creates a huge amount of workers

查看:64
本文介绍了ForkJoinPool 创造了大量的工人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ForkJoinPool 并行执行任务.当我查看程序的注销时,似乎 ForkJoinPool 创建了大量工作人员来执行我的任务(有如下所示的日志条目:05 Apr 2016 11:39:18,678 [ForkJoinPool-2-worker-2493] <message>).

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>).

创建的每个任务是否都有一个工作人员,然后根据我在 ForkJoinPool 中配置的并行数执行,或者我做错了什么?这是我的做法:

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

推荐答案

任务名称末尾的数字与池实际使用的线程数无关.看一下 ForkJoinPool 类的 registerWorker 方法.它看起来像这样:

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 被初始化为

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

如果您想测量池使用的实际线程数,最好记录 getPoolSize() 返回的内容.

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

这篇关于ForkJoinPool 创造了大量的工人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆