协程是一种线程框架,它运行在线程上,每一个协程就是一个耗时任务,协程内部程序是按顺序执行,它是一种非阻塞式的,用户能够手动控制协程的挂起和运行;android
官方介绍: 协程经过将复杂性放入库来简化异步编程。程序的逻辑能够在协程中顺序地表达,而底层库会为咱们解决其异步性。该库能够将用户代码的相关部分包装为回调、订阅相关事件、在不一样线程(甚至不一样机器)上调度执行,而代码则保持如同顺序执行同样简单;编程
引入bash
kotlin{
experimental {
coroutines 'enable'
}
}
// 协程
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
复制代码
GlobalScope.launch {
for(i in 1..3){
println("协程任务1打印: $i")
}
delay(200)
for(i in 1..3){
println("协程任务2打印: $i")
}
}
for(i in 1..3){
println("主线程打印: $i")
}
复制代码
能够经过日志看到协程是按照顺序执行:闭包
2020-05-26 15:30:58.673 24381-24381/? I/System.out: 主线程打印: 1
2020-05-26 15:30:58.673 24381-24381/? I/System.out: 主线程打印: 2
2020-05-26 15:30:58.673 24381-24381/? I/System.out: 主线程打印: 3
2020-05-26 15:30:58.674 24381-24446/? I/System.out: 协程任务1打印: 1
2020-05-26 15:30:58.674 24381-24446/? I/System.out: 协程任务1打印: 2
2020-05-26 15:30:58.674 24381-24446/? I/System.out: 协程任务1打印: 3
2020-05-26 15:30:58.878 24381-24448/? I/System.out: 协程任务2打印: 1
2020-05-26 15:30:58.878 24381-24448/? I/System.out: 协程任务2打印: 2
2020-05-26 15:30:58.878 24381-24448/? I/System.out: 协程任务2打印: 3
复制代码
源码框架
public fun CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job {
val newContext = newCoroutineContext(context)
val coroutine = if (start.isLazy)
LazyStandaloneCoroutine(newContext, block) else
StandaloneCoroutine(newContext, active = true)
coroutine.start(start, coroutine, block)
return coroutine
}
复制代码
CoroutineStart: 启动模式,默认就是DEFAULT,即建立协程就启动,还有一个LAZY,懒加载模式,须要调用start才会启动;异步
block: 闭包方法体,定义协程内须要执行的操做;async
Job: 建立协程的返回值,能够把job当作协程对象自己;异步编程
Suspend: 用来修饰协程的方法,被修饰的方法能够被协程挂起,suspend修饰的方法只能被suspend修饰的方法调用;函数
GlobalScope.async: async和launch的区别就是async有返回值ui
GlobalScope.launch(Dispatchers.IO){
// 多协程间 suspend函数运行
val suspend2 = GlobalScope.async(Dispatchers.IO){
return@async suspend2()
}
println("hello ${suspend2.isActive}")
println("hello ${suspend2.isActive} ${suspend2.await()}")
println("hello ${suspend2.isActive}")
// println("hello $suspend1 $suspend2 thread:${Thread.currentThread().name}")
}
复制代码
打印结果
2020-05-26 19:23:42.426 9411-9485/? I/System.out: hello true
2020-05-26 19:23:42.427 9411-9487/? I/System.out: hello suspend2 start, thread:DefaultDispatcher-worker-3
2020-05-26 19:23:42.441 9411-9485/? I/System.out: hello suspend2 end, thread:DefaultDispatcher-worker-1
2020-05-26 19:23:42.442 9411-9485/? I/System.out: hello true suspend2
2020-05-26 19:23:42.442 9411-9485/? I/System.out: hello false
复制代码