gpt4 book ai didi

kotlin - `SendChannel.offer` 、 `CompletableDeferred.complete` 等可以在协程之外调用吗?

转载 作者:行者123 更新时间:2023-12-02 13:38:52 26 4
gpt4 key购买 nike

CompletableDeferred文件说

All functions on this interface and on all interfaces derived from it are thread-safe and can be safely invoked from concurrent coroutines without external synchronization.



在任何协程之外调用这些函数是否安全?

对于 SendChannel<E> , offerclose不是 suspend所以它们可以在语法上在协程之外被调用,但这样做真的安全吗?

如果需要协程,最便宜的启动方式是什么: launch(Unconfined) ?

最佳答案

调用 offer 是安全的和 close从任何地方。这就是文档用“线程安全”短语所说的意思。

这些方法包含在 channel API 中的原因之一是能够将协程与基于各种回调和事件处理程序的常规非协程世界集成。您可以在 guide on UI programming with coroutines 中查看此类集成的实际示例。 .

关于kotlin - `SendChannel.offer` 、 `CompletableDeferred.complete` 等可以在协程之外调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47654180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com