Is there a good implementation of continuations in Java?
If so, what is the overhead like? The JVM wasn\'t designed with these sort of things in mind, right? So is t
Since Java 8, there is now a CompletableFuture
Consider the following example, where a Class offers a downloadAndResize
method:
public CompletableFuture downloadAndResize(String imageUrl, int width, int height) {
return CompletableFuture
.supplyAsync(() -> downloadImage(imageUrl))
.thenApplyAsync(x -> resizeImage(x, width, height));
}
private Image downloadImage(String url){
// TODO Download the image from the given url...
}
private Image resizeImage(Image source, int width, int height){
// TODO Resize the image to w / h
}
Usage of the above method could look like:
CompletableFuture imagePromise = downloadAndResize("http://some/url", 300, 200);
imagePromise.thenAccept(image -> {
// Gets executed when the image task has successfully completed
// do something with the image
});