【问题标题】:is this right to use coroutines in a non coroutine context这是在非协程上下文中使用协程的权利吗
【发布时间】:2019-11-14 10:25:37
【问题描述】:

有一个Processor 类,试图用协程替换一些代码。由于它在非协程上下文中,因此添加了val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob) 并用于启动协程。

在使用 Thread{}.start() 的地方添加了CoroutineScope,并使用了serviceScope.launch{}

在restart()函数内部,将CountDownLatch的使用替换为

serviceScope.launch {
                withContext(Dispatchers.IO) {

                    doReset()
                }
            }

问题:这个launch/withContext实际上并没有停止下一个if (!conDoProcess)的代码执行——所以它没有做latch以前做的事情。

doReset() 之前停止代码执行的正确方法是什么。完成了吗?

另一个问题,当处置这个Processor对象时,它调用serviceScope.cancel()

如果用serviceJob.cancel()打电话有什么区别?

class Processor {

    private val serviceJob = Job()
    private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob)

            .........

    /* return false if the it does not start the processing */
    fun restart(): Boolean {

        synchronized(_lock) {

            .........

            // 1.old code using latch to wait

            /******************
            val latch = CountDownLatch(1)
            streamThreadPoolExecutor.execute {

                doReset()  //

                latch.countDown()
            }
            latch.await(3, TimeUnit.SECONDS) // wait at most for 3 seconds if no one calls countDown

            *******************/

            // 2. change to using coroutines to suspend

            serviceScope.launch {
                withContext(Dispatchers.IO) {

                    doReset()
                }
            }

            // wait until reset is done
            if (!conDoProcess) {// the doRest() should update conDoProcess
                return false
            }

            for (i in providers.indices) {
                val pr = provider[i]
                serviceScope.launch {
                    pr.doProcess()
                }
            }

            return true
        }
    }

    fun dispose() {
        synchronized(_lock) {

            .........
            serviceScope.cancel()

            // or should it use
            // serviceJob.cancel()
            //==========>
        }
    }
}

【问题讨论】:

    标签: kotlin-coroutines withcontext


    【解决方案1】:

    我认为它使用了serviceScope.launch错误,它应该包括阻塞部分withContext(Dispatchers.IO)之后的其余部分,但在serviceScope.launch内部。

            // 2. change to using coroutines to suspend
    
            serviceScope.launch {
                withContext(Dispatchers.IO) {
    
                    doReset()
                }
    
                // wait until reset is done
                if (!conDoProcess) {// the doRest() should update conDoProcess
                  return false
                }
    
                for (i in providers.indices) {
                  val pr = provider[i]
                  serviceScope.launch {
                      pr.doProcess()
                  }
                }   
            }
    
            return true
    

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      相关资源
      最近更新 更多