Fork 不会(必然)创建新线程。在我的基准测试中,它只为每个可用内核创建 1 个线程 + 1 个额外线程。附加基准;Factorizer.factorize(71236789143834319L)
比如说,从 main() 调用。
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class Factorizer extends RecursiveTask<Long> {
static ForkJoinPool fjPool = new ForkJoinPool();
static final int sequentialThreshold = 10000;
private long number, low, high;
Factorizer(long number, long low, long high) {
this.number = number; this.low = low; this.high = high;
}
private long factorize() {
if ((number % 2) == 0) {
return 2;
}
// ensures i is odd (we already know number is not even)
long i = ((low % 2) == 0) ? low + 1: low;
for (/**/; i < high; i+=2) {
if ((number % i) == 0) {
return i;
}
}
return number;
}
@Override
protected Long compute() {
// ugly debug statement counts active threads
System.err.println(Thread.enumerate(
new Thread[Thread.activeCount()*2]));
if (high - low <= sequentialThreshold) {
return factorize();
} else {
long mid = low + (high - low) / 2;
Factorizer left = new Factorizer(number, low, mid);
Factorizer right = new Factorizer(number, mid, high);
left.fork();
return Math.min(right.compute(), left.join());
}
}
static long factorize(long num) {
return fjPool.invoke(new Factorizer(num, 2, (long)Math.sqrt(num+1)));
}
}
注意 - 这只是一个测试。不要使用此代码来认真尝试考虑任何重要因素。