赞
踩
//Kotlin支持库 implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.32" //协程核心库 implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2" //协程Android支持库 implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2" //协程Java8支持库 implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.3" //ViewModelScope implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" //LifecycleScope implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0" //LiveData implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
普通程序:
fun getList(): List<Int> { val list = mutableListOf<Int>() list.add(1) println("add 1") list.add(2) println("add 2") list.add(3) println("add 3") return list } fun printList(list: List<Int>) { list.forEach { println("print $it") } } fun main() { val list = getList() printList(list) } //add 1 //add 2 //add 3 //print 1 //print 2 //print 3
协程:
fun getSequence() = sequence { println("add 1") yield(1) println("add 2") yield(2) println("add 3") yield(3) } fun printSequence(sequence: Sequence<Int>) { val iterator = sequence.iterator() while (iterator.hasNext()) { val i = iterator.next() println("print $i") } } fun main() { val sequence = getSequence() printSequence(sequence) } //add 1 //print 1 //add 2 //print 2 //add 3 //print 3
区别:
Kotlin 协程框架将线程池进一步封装,对开发者暴露出统一的协程API。
线程:
//开启2个线程
fun main() {
println(Thread.currentThread().name)
thread {
println(Thread.currentThread().name)
Thread.sleep(100L)
}
Thread.sleep(1000L)
}
//main
//Thread-0
协程:
配置VM参数:-Dkotlinx.coroutines.debug
//启动2个协程
fun main() = runBlocking {
println(Thread.currentThread().name)
launch {
println(Thread.currentThread().name)
delay(100L)
}
Thread.sleep(1000L)
}
//main @coroutine#1
//main @coroutine#2
区别:
协程可以理解为运行在线程当中的、更加轻量的 Task。
线程:
// 10亿线程 fun main() { repeat(1000_000_000) { thread { Thread.sleep(1000_000L) } } Thread.sleep(10_000L) } /* 输出结果: Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:717) at kotlin.concurrent.ThreadsKt.thread(Thread.kt:42) at kotlin.concurrent.ThreadsKt.thread$default(Thread.kt:20) */
协程:
// 10亿协程
fun main() = runBlocking {
repeat(1000_000_000) {
launch {
delay(1000000)
}
}
delay(10000L)
}
/*
运行结果:
正常
*/
区别:
fun main() = runBlocking(Dispatchers.IO) { repeat(3) { launch { repeat(3) { println(Thread.currentThread().name) delay(100L) } } } delay(5000L) } //DefaultDispatcher-worker-2 @coroutine#2 //DefaultDispatcher-worker-6 @coroutine#4 //DefaultDispatcher-worker-4 @coroutine#3 //DefaultDispatcher-worker-3 @coroutine#4 //DefaultDispatcher-worker-6 @coroutine#3 //DefaultDispatcher-worker-1 @coroutine#2 //线程切换了 //DefaultDispatcher-worker-1 @coroutine#4 //DefaultDispatcher-worker-3 @coroutine#2 //线程切换了 //DefaultDispatcher-worker-6 @coroutine#3
说明:协程coroutine#2
执行了3次,每次都在不同的线程上。
线程:
fun main() { repeat(3) { Thread.sleep(1000L) println("print-A: ${Thread.currentThread().name}") } repeat(3) { Thread.sleep(900L) println("print-B: ${Thread.currentThread().name}") } } //print-A: main //print-A: main //print-A: main //print-B: main //print-B: main //print-B: main
协程:
fun main() = runBlocking { launch { repeat(3) { delay(1000L) println("print-A: ${Thread.currentThread().name}") } } launch { repeat(3) { delay(900L) println("print-B: ${Thread.currentThread().name}") } } delay(3000L) } //print-B: main @coroutine#3 //print-A: main @coroutine#2 //print-B: main @coroutine#3 //print-A: main @coroutine#2 //print-B: main @coroutine#3 //print-A: main @coroutine#2
说明:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。