繁体   English   中英

Kotlin 的 GlobalScope (Coroutine) 中的代码无法正常工作

[英]Code inside Kotlin's GlobalScope (Coroutine) is not working properly

我正在尝试制作快速排序算法的可视化工具,算法的功能很好,但是当我将代码放在 GlobalScope 中以便我可以使用延迟来可视化事物时,算法的行为有所不同。 这是我的功能....

//Quick Sort Part
private suspend fun partition(arr: MutableList<Button>, low: Int, high: Int): Int {
    //If I comment this job, and run the code the algorithm works
    val job2 = GlobalScope.launch(Dispatchers.Main) {
        var low = low
        var high = high
        var pivot = arr[(low + high) / 2].layoutParams.height
       
        while (low <= high) {
            Log.d(TAG, "partition: $low  $high")
            while (arr[low].layoutParams.height < pivot) {
                low++
            }
            while (arr[high].layoutParams.height > pivot) {
                high--
            }

            if (low <= high) {
                val temp = arr[low].layoutParams.height
                arr[low].layoutParams.height = arr[high].layoutParams.height
                arr[high].layoutParams.height = temp
                root_layout.requestLayout()
                low++
                high--
            }
        }
    }
    job2.join()
    return low
}


private fun quickSort(arr: MutableList<Button>, low: Int, high: Int) {
    val job1 = GlobalScope.launch(Dispatchers.Main) {
        val pi = partition(arr, low, high)
        if (low < pi - 1) {
            quickSort(arr, low, pi - 1)
        }
        if (pi < high) {
            quickSort(arr, pi, high)
        }
    }

}

这是我使用协程的时候

这是 function 的结果,如果我不使用协程,这工作正常,所有条形图都按递增顺序排序。

因为您已经在启动 scope 中定义了 low 和 high 变量,并在那里更新它但仍然返回作为参数接收的变量。

在外部范围内定义它:

var low = low  // define the variable in the outer scope
val job2 = GlobalScope.launch(Dispatchers.Main) {  // ...

但是阴影不是最好的方法(它很容易出错,因为它欺骗了你),为了简单起见,只是给它起一个别的名字。

提示:使用主线程通常用于 CPU 密集度较低的任务,应该主要用于显示内容和更新 UI。 对于计算任务,请随意使用 Dispatchers.Default!

此外,使用 Jobs 执行任务并加入它们并没有比withContext优化得更好,后者旨在轻松地从另一个 Dispatching 线程返回结果,请改用它!

private suspend fun partition(arr: MutableList<Button>, low: Int, high: Int): Int {
    return withContext(Dispatchers.Default) {
        var low = low
        var high = high
        var pivot = arr[(low + high) / 2].layoutParams.height
       
        while (low <= high) {
            Log.d(TAG, "partition: $low  $high")
            while (arr[low].layoutParams.height < pivot) {
                low++
            }
            while (arr[high].layoutParams.height > pivot) {
                high--
            }

            if (low <= high) {
                val temp = arr[low].layoutParams.height
                arr[low].layoutParams.height = arr[high].layoutParams.height
                arr[high].layoutParams.height = temp
                root_layout.requestLayout()
                low++
                high--
            }
        }
        low  //^withContext
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM