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
一起使用,是否有我应该注意的注意事项