Skip to content

Instantly share code, notes, and snippets.

@am4dr
Last active July 20, 2017 01:43
Show Gist options
  • Save am4dr/3b3303abaf5196157a84d8383bbcd2dc to your computer and use it in GitHub Desktop.
Save am4dr/3b3303abaf5196157a84d8383bbcd2dc to your computer and use it in GitHub Desktop.
FutureTask with CompletionStage
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