Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在CompletableFuture中使用ManagedBlocker的注意事项?_Java_Java.util.concurrent_Completable Future_Forkjoinpool - Fatal编程技术网

Java 在CompletableFuture中使用ManagedBlocker的注意事项?

Java 在CompletableFuture中使用ManagedBlocker的注意事项?,java,java.util.concurrent,completable-future,forkjoinpool,Java,Java.util.concurrent,Completable Future,Forkjoinpool,我想将ManagedBlocker与CompletableFuture一起使用,以防止ForkJoinPool.commonPool耗尽,即: @Test public void testCompletableFutureWithManagedBlocker() throws ExecutionException, InterruptedException { final long startingTime = System.currentTimeMillis(); final

我想将
ManagedBlocker
CompletableFuture
一起使用,以防止
ForkJoinPool.commonPool
耗尽,即:

@Test
public void testCompletableFutureWithManagedBlocker() throws ExecutionException, InterruptedException {
    final long startingTime = System.currentTimeMillis();
    final int numberOfFuture = 32;
    final CountDownLatch countDownLatch = new CountDownLatch(numberOfFuture);

    final List<CompletableFuture<Void>> futures = Stream
            .generate(() -> CompletableFuture.runAsync(() -> {
                countDownLatch.countDown();
                BlockingTasks.callInManagedBlock((() -> {sleep(); return null; }));
            }))
            .limit(numberOfFuture)
            .collect(Collectors.toList());

    futures.forEach((future) -> {
        try { countDownLatch.await(); } catch (InterruptedException ignored) {}
        future.join();
    });

    System.out.println("Time taken roughly: [" + (System.currentTimeMillis() - startingTime) + "]ms");
}

public class BlockingTasks {

    public static<T> T callInManagedBlock(final Supplier<T> supplier) {
        final SupplierManagedBlock<T> managedBlock = new SupplierManagedBlock<>(supplier);
        try {
            ForkJoinPool.managedBlock(managedBlock);
        } catch (InterruptedException e) {
            throw new Error(e);
        }
        return managedBlock.getResult();
    }

    private static class SupplierManagedBlock<T> implements ForkJoinPool.ManagedBlocker {
        private final Supplier<T> supplier;
        private T result;
        private boolean done = false;

        private SupplierManagedBlock(final Supplier<T> supplier) {
            this.supplier = supplier;
        }

        @Override
        public boolean block() {
            result = supplier.get();
            done = true;
            return true;
        }

        @Override
        public boolean isReleasable() {
            return done;
        }

        public T getResult() {
            return result;
        }
    }
}
@测试
public void testCompletableFutureWithManagedBlocker()引发ExecutionException、InterruptedException{
最终长启动时间=System.currentTimeMillis();
最终整数未来=32;
最终CountDownLatch CountDownLatch=新的CountDownLatch(numberOfFuture);
最终列表期货=流
.generate(()->CompletableFuture.runAsync(()->{
countdownlock.countDown();
callInManagedBlock((()->{sleep();返回null;}));
}))
.限制(未来数量)
.collect(Collectors.toList());
futures.forEach((未来)->{
请尝试{countDownLatch.await();}catch(InterruptedException被忽略){}
future.join();
});
System.out.println(“大约花费的时间:[”+(System.currentTimeMillis()-startingTime)+“]ms”);
}
公共类封锁任务{
公共静态T callInManagedBlock(最终供应商){
最终供应商managedBlock managedBlock=新供应商managedBlock(供应商);
试一试{
managedBlock(managedBlock);
}捕捉(中断异常e){
抛出新错误(e);
}
返回managedBlock.getResult();
}
私有静态类SupplierManagedBlock实现ForkJoinPool.ManagedBlock{
私人最终供应商;
私人T结果;
私有布尔完成=假;
私人供应商ManagedBlock(最终供应商){
此项。供应商=供应商;
}
@凌驾
公共布尔块(){
结果=supplier.get();
完成=正确;
返回true;
}
@凌驾
公共布尔值可删除(){
已完成的返回;
}
公共T getResult(){
返回结果;
}
}
}
对于将
ManagedBlocker
CompletableFuture
一起使用,是否有我应该注意的注意事项