CompletableFuture.supplyAsync(
() -> {
transporter.write(req);
//here take the value from a blocking queue,will throw a interruptedException
return responseQueue.take();
}, executorService);
The common method to deal with interruptedException is either to interrupt again or direct throw interruptedException, but both cannot work. Anyone have the idea?
@antak mentioned it buried in a comment, but I think the correct answer here is:
So the sample code would look something like:
As lambda functions don't support throwing exceptions, I think Java developers will need a new paradigm. One thing that comes to mind is as follows:
Lambda functions can return instances of this wrapper. (Edit: your case)
I change the code like this.
I ran into the same question, but after reading more from comments here and reference book I think you can do either one of these two:
1 (what I end up doing):
or 2:
I know the 2nd one does not use the
executorService
, but I feel the whole point of using CompletableFuture is utilizing the CompletionStage APIs in functional-style.