下面列出了java.util.concurrent.ForkJoinTask#inForkJoinPool ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void compute()
{
if(endValue - startValue > MAX_ELEMENTS_TO_PROCESS) // If there are too many elements to process in one operation...
{
if(ForkJoinTask.inForkJoinPool()) // ... and if we are in a ForkJoinPool ...
{
int halfWay = (endValue + startValue) / 2;
invokeAll(new ArrayDoubler(array, startValue, halfWay), new ArrayDoubler(array, halfWay, endValue));
return;
}
}
for(int i = startValue; i < endValue; i++) // If we aren't in a ForkJoinPool or if there are not a large number of elements to be processed
{
array[i] = array[i] * 2;
}
}
@Override
public void run() {
FibonacciResult result;
if(ForkJoinTask.inForkJoinPool()) result = new FibonacciResult(true, new FibonacciTaskFJ(n).compute());
else result = new FibonacciResult(false, fib(n));
setResult(result);
}
@Override
protected void executeRunnable(WrappedRunnable runnable) {
if (ForkJoinTask.inForkJoinPool()) {
@SuppressWarnings("unused")
Future<?> possiblyIgnoredError = ForkJoinTask.adapt(runnable).fork();
} else {
super.executeRunnable(runnable);
}
}
public void submit(final ForkJoinTask<?> t) {
if (ForkJoinTask.inForkJoinPool()) {
ForkJoinTask.invokeAll(t);
// alternatively just pool().pool.execute(t), which simply forces it to be this pool
// (right now we expect only one anyway)
} else {
pool.submit(t);
}
}
private static int availableParallelism() {
return ForkJoinTask.inForkJoinPool()
? ForkJoinTask.getPool().getParallelism()
: ForkJoinPool.getCommonPoolParallelism();
}
private static int availableParallelism() {
return ForkJoinTask.inForkJoinPool()
? ForkJoinTask.getPool().getParallelism()
: ForkJoinPool.getCommonPoolParallelism();
}
/**
* Creates a new MiGzInputStream that will read MiGz-compressed bytes from the specified underlying
* inputStream using the default number of threads. Worker tasks will execute on the current {@link ForkJoinPool}
* returned by {@link ForkJoinTask#getPool()} if applicable, the {@link ForkJoinPool#commonPool()} otherwise,
* with a maximum number of concurrent workers equal to the target parallelism of the pool.
*
* @param inputStream the stream from which compressed bytes will be read
* @throws UncheckedIOException if a problem occurs reading the block size header
*/
public MiGzInputStream(InputStream inputStream) {
this(inputStream, ForkJoinTask.inForkJoinPool() ? ForkJoinTask.getPool() : ForkJoinPool.commonPool());
}
/**
* Creates a new MiGzOutputStream that will output MiGz-compressed bytes to the specified underlying
* outputStream using the default block size. Worker tasks will execute on the current {@link ForkJoinPool} returned
* by {@link ForkJoinTask#getPool()} if applicable, or the {@link ForkJoinPool#commonPool()} otherwise.
*
* @param outputStream the stream to which compressed bytes will be written
*/
public MiGzOutputStream(OutputStream outputStream) {
this(outputStream, ForkJoinTask.inForkJoinPool() ? ForkJoinTask.getPool() : ForkJoinPool.commonPool(),
DEFAULT_BLOCK_SIZE);
}