C#Task.ContinueWith()vs java?

win*_*ndc 6 c# java guava completable-future

在C#中,Task类有ContinueWith方法,当任务运行完成状态时,ContinueWith方法将被调用,而在JAVA中,是否有一些方法如ContinueWith

我知道番石榴listenablefuture,但它使用一个新的线程来等待'任务'完成,它是否等于C# ContinueWith

和JAVA 8 具有相同的效果,那么C#的区别是 什么?CompletableFuture whenCompleteContinueWith listenablefuture CompletableFuture

谢谢!

Joe*_*e C 2

Task.Net 中的A和CompletableFutureJava 中的 A 实现了非常相似的目的。TaskScheduler它们提供对在不同线程( .Net 或Java 中)中执行的某些内容的引用Executor,并能够等待结果和/或在完成时执行不同的操作。

There are a couple of differences between Task.ContinueWith and CompletableFuture.whenComplete. For one, the function passed into the .Net version takes the Task itself as an argument, requiring an additional step to get its result (or to propagate downstream exceptions if necessary). The function passed into Java version takes the result of the task as an argument.

The other significant difference is the thread on which the continuation task will run. In .Net, it will by default run on a different thread from the main task. In Java, it will by default run on the same thread as the main task. These default actions can be overridden in both cases.