在kotlinx。协程库,您可以使用启动(使用join)或异步(使用await)启动新的协程。它们之间的区别是什么?


当前回答

启动返回一个作业

Async返回一个结果(延迟作业)

使用join启动用于等待作业完成。它只是挂起调用join()的协程,同时让当前线程空闲地做其他工作(比如执行另一个协程)。

Async用于计算一些结果。它创建一个协程,并将其未来结果作为Deferred的实现返回。当产生的延迟被取消时,正在运行的协程也被取消。

考虑一个返回字符串值的异步方法。如果async方法在没有await的情况下使用,它将返回一个Deferred字符串,但如果使用await,则将得到一个字符串作为结果


async和launch之间的关键区别: Deferred在协程完成执行后返回类型为T的特定值,而Job则不会。

其他回答

除了其他很好的答案,对于熟悉Rx和进入协同程序的人来说,async返回一个类似于Single的Deferred,而launch返回一个更类似于Completable的Job。您可以.await()阻塞并获取第一个的值,.join()阻塞直到Job完成。

launch is used to fire and forget coroutine. It is like starting a new thread. If the code inside the launch terminates with exception, then it is treated like uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications. join is used to wait for completion of the launched coroutine and it does not propagate its exception. However, a crashed child coroutine cancels its parent with the corresponding exception, too. async is used to start a coroutine that computes some result. The result is represented by an instance of Deferred and you must use await on it. An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed. You MUST NOT forget about the coroutine you’ve started with async.

both coroutine builders namely launch and async are basically lambdas with receiver of type CoroutineScope which means their inner block is compiled as a suspend function, hence they both run in an asynchronous mode AND they both will execute their block sequentially. The difference between launch and async is that they enable two different possibilities. The launch builder returns a Job however the async function will return a Deferred object. You can use launch to execute a block that you don't expect any returned value from it i.e writing to a database or saving a file or processing something basically just called for its side effect. On the other hand async which return a Deferred as I stated previously returns a useful value from the execution of its block, an object that wraps your data, so you can use it for mainly its result but possibly for its side effect as well. N.B: you can strip the deferred and get its value using the function await, which will block the execution of your statements until a value is returned or an exceptions is thrown! You could achieve the same thing with launch by using the function join() both coroutine builder (launch and async) are cancelable. anything more?: yep with launch if an exception is thrown within its block, the coroutine is automatically canceled and the exceptions is delivered. On the other hand, if that happens with async the exception is not propagated further and should be caught/handled within the returned Deferred object. more on coroutines https://kotlinlang.org/docs/tutorials/coroutines/coroutines-basic-jvm.html https://www.codementor.io/blog/kotlin-coroutines-6n53p8cbn1

启动返回一个作业

Async返回一个结果(延迟作业)

使用join启动用于等待作业完成。它只是挂起调用join()的协程,同时让当前线程空闲地做其他工作(比如执行另一个协程)。

Async用于计算一些结果。它创建一个协程,并将其未来结果作为Deferred的实现返回。当产生的延迟被取消时,正在运行的协程也被取消。

考虑一个返回字符串值的异步方法。如果async方法在没有await的情况下使用,它将返回一个Deferred字符串,但如果使用await,则将得到一个字符串作为结果


async和launch之间的关键区别: Deferred在协程完成执行后返回类型为T的特定值,而Job则不会。

启动/异步没有结果

用在不需要结果的时候, 不要阻塞被调用的代码, 按顺序运行

Async for result

当需要等待结果时,可以并行运行 效率, 阻塞被调用的代码, 并发运行