ForkJoinPool 创建了大量的工人
ForkJoinPool creates a huge amount of workers
我使用 ForkJoinPool 并行执行任务。当我查看程序的注销时,似乎 ForkJoinPool 创建了大量工作人员来执行我的任务(日志条目如下所示:05 Apr 2016 11:39:18,678 [ForkJoinPool-2-worker-2493] <message>
)。
是否为每个创建的任务创建了一个工作程序,然后根据我在 ForkJoinPool 中配置的并行数执行,或者我做错了什么?这是我的做法:
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方法class。它看起来像这样:
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() returns。
我使用 ForkJoinPool 并行执行任务。当我查看程序的注销时,似乎 ForkJoinPool 创建了大量工作人员来执行我的任务(日志条目如下所示:05 Apr 2016 11:39:18,678 [ForkJoinPool-2-worker-2493] <message>
)。
是否为每个创建的任务创建了一个工作程序,然后根据我在 ForkJoinPool 中配置的并行数执行,或者我做错了什么?这是我的做法:
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方法class。它看起来像这样:
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() returns。