Overview of Advanced Java 8 CompletableFuture Features (Part 3)

preview_player
Показать описание
This video walks through an example of applying many Java 8 completion stage methods, as well as showing how to handle exceptions in chains of completion stage methods.
Рекомендации по теме
Комментарии
Автор

I impletemeted CommonPoolCompletionService use whenComplete hook

similar to ExecutorCompletionService
public class {
private final BlockingQueue<V> completionQueue;
public CommonPoolCompletionService() {
this.completionQueue = new LinkedBlockingQueue<>();
}

public CompletableFuture<V> submit(AsyncCallable<V> asyncTask) {
CompletableFuture<V> future = -> {
try {
return asyncTask.asyncCall();
} catch(InterruptedException e) {
e.printStackTrace();
return null;
}
});
future.whenComplete((r, e) -> {
completionQueue.add(r);
});

return future;
}

public V take() throws InterruptedException {
return completionQueue.take();
}

public V poll() {
return completionQueue.poll();
}

public V poll(long timeout, TimeUnit unit) throws InterruptedException {
return completionQueue.poll(timeout, unit);
}
}

americannumber