Tag: 协同程序

Kotlin – exception后继续协程

我的问题 exception之后是否可以继续执行协程? 例 try { someMethod1() someMethod2() //Throws an exception! someMethod3() } catch(e: Exception) { //I do not want to call someMethod3 here! //I want the coroutine to resume after the exception inside of the original block. } finally { //I also do not want to call someMethod3 here! //I want the coroutine to resume […]

为什么operation.map(启动线程).foreach(join())在kotlin中工作?

我一直在试图find一个解释,为什么这在kotlin工程: (1..100).map { launch { System.out.println(“Hello from on ${Thread.currentThread().name}”) delay(100) } }.forEach { it.join() } 在Java中,这将: 开始线程1 加入线程1 – 在这里阻塞,永远不会启动超过1个线程。 在kotlin这个进程并行的multithreading。 为什么这个工作?

Kotlin中线程与协程的区别

Kotlin中是否有任何特定的语言实现与其他语言的协同程序有所不同? 这意味着协程就像轻量级的线程? 有什么不同? kotlin的协同程序是否并行/同时运行? 即使在多核系统中,在任何给定的时间只有一个协程运行(是不是?) 这里我开始了100000个协程,这个代码背后发生了什么? for(i in 0..100000){ async(CommonPool){ //run long running operations } }

如何使用Kotlin协程等待()在主线程上

我刚开始学习Kotlin协同程序,并试图模拟一些长时间的API调用,并在UI上显示结果: class MainActivity : AppCompatActivity() { fun log(msg: String) = println(“[${Thread.currentThread().name}] $msg”) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.setContentView(R.layout.activity_main) val resultTV = findViewById(R.id.text) as TextView val a = async(CommonPool) { delay(1_000L) 6 } val b = async(CommonPool) { delay(1_000L) 7 } launch() { val aVal = a.await() val bVal = b.await() resultTV.setText((aVal * bVal).toString()) } […]

在界面中声明最后的乐趣

我正在Kotlin编写一个合作的multithreading引擎。 我试图写一个接口,如下所示: interface Processor { var suspendAction: (Continuation) -> Unit inline suspend fun yield() = suspendCoroutine(suspendAction) suspend fun process(inbox: Inbox) = Unit } yield()是我想提供给所有这个接口的实现者的服务。 由于每个虚拟呼叫站点代表了内联的障碍,并且由于每个进入suspend fun入口都有其代价,出于性能的原因,我需要这个函数是final ,但是Kotlin不允许我这样做。 我发现一个解决方法将yield()转换为扩展的乐趣: inline suspend fun Processor.yield() = suspendCoroutine(suspendAction) 我想问一下,这样的用例是否会激励Kotlin语言设计师在interface final fun 。 请注意,与典型的等待IO暂停方案不同,此处的yield()发生在CPU密集型热点线程上。

我可以通过在Java代码中使用它们来利用Kotlin的协同程序吗?

我的目标是什么? 我的目标是能够使用Java的Kotlin的Coroutine系统。 我希望能够暂停执行一段时间,然后在给定的时间过去之后在那个位置重新找回。 从Java,我希望能够执行任务,而不是以异步方式暂停执行中间执行,例如: //example 1 someLogic(); pause(3000L); //3 seconds someMoreLogic(); //example 2 while(true) { someContinuedLogic(); pause(10000L); //10 seconds } 我的问题是什么? 正如所料,我能够从Kotlin中完美地执行协程,但是对于Java来说,它变得棘手,因为代码的Java部分立即执行整个块而没有任何暂停,而Kotlin块正确地暂停1,并且然后4秒。 我的问题是什么? 甚至有可能使用Kotlin作为Java协程的主干? 如果是这样,我做错了什么? 下面你可以find显示我如何尝试在Java中使用Kotlin协程的源代码。 KtScript类 abstract class KtScript { abstract fun execute() fun async(block: suspend () -> T): CompletableFuture { val future = CompletableFuture() block.startCoroutine(completion = object : Continuation { override fun resume(value: […]

kotlin协程和timer是否有异步调用?

Kotlin是否有可能在一段时间内调用函数async(),女巫会在完成后返回默认结果? 我发现只有呼叫等待是可能的,而无限等待结果。 async { … val result = computation.await() … } 但真正的生产情况比你需要返回默认结果或exception。 在Kotlin协同程序中做什么是正确的方法? 像这样的东西: async { … val timeout = 100500 val result: SomeDeferredClass = computation.await(timeout) if (result.isTimeout()) { // get default value } else { // process result } … }