Kotlin runBlocking launch多個(gè)協(xié)程讀寫(xiě)mutableListOf時(shí)序
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
fun main(args: Array<String>) {
var lists = mutableListOf<String>()
runBlocking {
launch {
repeat(Int.MAX_VALUE) {
delay(1)
val len = (3..6).random()
lists.clear()
for (i in 0 until len) {
lists.add("$it@$i")
}
lists.forEach(::println)
}
}
launch {
repeat(Int.MAX_VALUE) {
delay(1)
lists.forEachIndexed { index, i ->
lists[index] = "a-${lists.size}-$it-$index"
}
println(lists)
}
}
launch {
repeat(Int.MAX_VALUE) {
delay(1)
lists.forEachIndexed { index, i ->
lists[index] = "b-${lists.size}-$it-$index"
}
println(lists)
}
}
}
}
kotlin的for循環(huán)遍歷_zhangphil的博客-CSDN博客kotlin的for循環(huán)遍歷。https://blog.csdn.net/zhangphil/article/details/130730729
kotlin協(xié)程,runBlocking里面的launch_zhangphil的博客-CSDN博客runBlocking 內(nèi)部啟動(dòng)的3個(gè)協(xié)程做耗時(shí)操作,從輸出可以看到3個(gè)協(xié)程交叉并發(fā)執(zhí)行,runBlocking 會(huì)等到3個(gè)協(xié)程執(zhí)行結(jié)束后才退出,輸出結(jié)果有明確先后順序。runBlocking 會(huì)等待相同作用域的協(xié)程完成才退出runBlocking 本身阻塞線程,但內(nèi)部運(yùn)行的協(xié)程又非阻塞。kotlin的runBlocking 當(dāng)內(nèi)部相同作用域的所有協(xié)程都運(yùn)行結(jié)束后,在 runBlocking 之后的代碼才能執(zhí)行, runBlocking 會(huì)阻塞所在線程。https://blog.csdn.net/zhangphil/article/details/131387354kotlin mutableListOf Collection_zhangphil的博客-CSDN博客Java for循環(huán)刪除ArrayList重復(fù)元素陷阱,Iterator迭代器遍歷刪除重復(fù)元素這是一個(gè)一不留神就犯下錯(cuò)誤的Java列表ArrayList重復(fù)刪除元素陷阱,并且問(wèn)題比較隱蔽。Java的Vector,Stack,ArrayList,LinkedList異同Collection的實(shí)現(xiàn)子類型是什么,它都支持iterator()函數(shù),該函數(shù)返回一個(gè)迭代器,該迭代器即可遍歷訪問(wèn)Collection中每一個(gè)元素。List是有序的Collection。和Set不同,List允許有相同元素。https://blog.csdn.net/zhangphil/article/details/131464487文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-652896.html
kotlin協(xié)程runBlocking 阻塞線程_zhangphil的博客-CSDN博客runBlocking 內(nèi)部啟動(dòng)的3個(gè)協(xié)程做耗時(shí)操作,從輸出可以看到3個(gè)協(xié)程交叉并發(fā)執(zhí)行,runBlocking 會(huì)等到3個(gè)協(xié)程執(zhí)行結(jié)束后才退出,輸出結(jié)果有明確先后順序。runBlocking 會(huì)等待相同作用域的協(xié)程完成才退出runBlocking 本身阻塞線程,但內(nèi)部運(yùn)行的協(xié)程又非阻塞。kotlin的runBlocking 當(dāng)內(nèi)部相同作用域的所有協(xié)程都運(yùn)行結(jié)束后,在 runBlocking 之后的代碼才能執(zhí)行, runBlocking 會(huì)阻塞所在線程。https://blog.csdn.net/zhangphil/article/details/129263455文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-652896.html
到了這里,關(guān)于Kotlin runBlocking launch多個(gè)協(xié)程讀寫(xiě)mutableListOf時(shí)序的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!