kotlin: 协程和线程的关系
一,代码:
binding.button2.setOnClickListener {
println("点击代码块开始:")
runBlocking { // 1
launch {
val threadId = Thread.currentThread().id
val threadName = Thread.currentThread().name
val context = coroutineContext + CoroutineName("launch1")
val dispatcher = context[ContinuationInterceptor]
val job = context[Job]
val name = context[CoroutineName]
val coInfo = "context:"+context+";job:"+job+";name:"+name+";dispatcher:"+dispatcher
println("launch内1:线程id:${threadId} 线程name:${threadName},协程: ${coInfo}")
delay(1000)
}
launch {
val threadId = Thread.currentThread().id
val threadName = Thread.currentThread().name
val context = coroutineContext + CoroutineName("launch2")
val dispatcher = context[ContinuationInterceptor]
val job = context[Job]
val name = context[CoroutineName]
val coInfo = "context:"+context+";job:"+job+";name:"+name+";dispatcher:"+dispatcher
println("launch内2:线程id:${threadId} 线程name:${threadName},协程: ${coInfo}")
delay(1000)
}
for (i in 0..3) {
val threadId = Thread.currentThread().id
val threadName = Thread.currentThread().name
val context = coroutineContext + CoroutineName("blocking"+i)
val dispatcher = context[ContinuationInterceptor]
val job = context[Job]
val name = context[CoroutineName]
val coInfo = "context:"+context+";job:"+job+";name:"+name+";dispatcher:"+dispatcher
println("launch外:i:${i},线程id:${threadId} 线程name:${threadName},协程: ${coInfo}")
delay(1000)
}
}
val threadId = Thread.currentThread().id
val threadName = Thread.currentThread().name
println("runblocking外:线程id:${threadId} 线程name:${threadName}")
println("点击代码块结束")
}
二,测试效果:
从运行结果可以看到:
runblocking内的代码在一个协程内运行
launch内代码会启动一个新的协程运行
而对应的线程一直没变,说明多个协程可以在一个线程内切换运行
I 点击代码块开始:
I launch外:i:0,线程id:2 线程name:main,协程: context:[BlockingCoroutine{Active}@a697fe9, CoroutineName(blocking0), BlockingEventLoop@b1aa46e];
job:BlockingCoroutine{Active}@a697fe9;name:CoroutineName(blocking0);dispatcher:BlockingEventLoop@b1aa46e
I launch内1:线程id:2 线程name:main,协程: context:[StandaloneCoroutine{Active}@385b70f, CoroutineName(launch1), BlockingEventLoop@b1aa46e];
job:StandaloneCoroutine{Active}@385b70f;name:CoroutineName(launch1);dispatcher:BlockingEventLoop@b1aa46e
I launch内2:线程id:2 线程name:main,协程: context:[StandaloneCoroutine{Active}@24fb39c, CoroutineName(launch2), BlockingEventLoop@b1aa46e];
job:StandaloneCoroutine{Active}@24fb39c;name:CoroutineName(launch2);dispatcher:BlockingEventLoop@b1aa46e
I launch外:i:1,线程id:2 线程name:main,协程: context:[BlockingCoroutine{Active}@a697fe9, CoroutineName(blocking1), BlockingEventLoop@b1aa46e];
job:BlockingCoroutine{Active}@a697fe9;name:CoroutineName(blocking1);dispatcher:BlockingEventLoop@b1aa46e
I launch外:i:2,线程id:2 线程name:main,协程: context:[BlockingCoroutine{Active}@a697fe9, CoroutineName(blocking2), BlockingEventLoop@b1aa46e];
job:BlockingCoroutine{Active}@a697fe9;name:CoroutineName(blocking2);dispatcher:BlockingEventLoop@b1aa46e
I launch外:i:3,线程id:2 线程name:main,协程: context:[BlockingCoroutine{Active}@a697fe9, CoroutineName(blocking3), BlockingEventLoop@b1aa46e];
job:BlockingCoroutine{Active}@a697fe9;name:CoroutineName(blocking3);dispatcher:BlockingEventLoop@b1aa46e
I runblocking外:线程id:2 线程name:main
I 点击代码块结束