Last active
July 20, 2017 01:43
-
-
Save am4dr/3b3303abaf5196157a84d8383bbcd2dc to your computer and use it in GitHub Desktop.
FutureTask with CompletionStage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.CompletableFuture | |
import java.util.concurrent.FutureTask | |
import java.util.concurrent.Callable | |
def task = new FutureTask({ | |
println "runAsync: ${Thread.currentThread()}" | |
def i = 0 | |
for (; i < 5; ++i) { | |
println "count: $i" | |
Thread.sleep(200) | |
} | |
return i - 1 | |
} as Callable<Integer>) | |
def future = CompletableFuture.supplyAsync { task.run(); task.get() } | |
.thenAccept { println "completed with $it" } | |
.exceptionally { println "exception: $it" } | |
Thread.sleep(500) | |
task.cancel(true) | |
println "await" | |
future.get() | |
println "finished" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment