北流网站,j2ee 建设简单网站,市场营销策略,兰州手机网站一、什么叫协程
协程可以称为轻量级线程#xff0c;线程代码块#xff1b;
二、GlobalScope
协程 CoroutineScope (协程作用域) 的上下文中通过 launch、async 等构造器来启动。GlobalScope ,即全局作用域内启动了一个新的协程#xff0c;这意味这该协程的生命周期只受整…一、什么叫协程
协程可以称为轻量级线程线程代码块
二、GlobalScope
协程 CoroutineScope (协程作用域) 的上下文中通过 launch、async 等构造器来启动。GlobalScope ,即全局作用域内启动了一个新的协程这意味这该协程的生命周期只受整个应用程序的生命周期的限制即只要整个应用程序还在运行中只要协程的任务还未结束该协程就可以一直运行。 //在后台启动一个新协程GlobalScope.launch {delay(3000)//非阻塞式延迟println(World!)}println(Hello )Thread.sleep(4000)//主线程阻塞 4秒以此保证JVM 存活
输出 Hello World! 三、runBlocking
非阻塞代码 delay() 和 阻塞代码 Thread.sleep() ,使得我们很容易就搞混当前程序是否是阻塞的可以改为 runBlocking 来明确这种情形。
fun test2(){GlobalScope.launch {delay(1000)println(World)}println(Hello )runBlocking {delay(2000)}
}
运行结果和第一个程序一样但是这段代码只使用了非阻塞延迟。主线程调用了 runBlocking 函数直到 runBlocking 内部的所有协程执行完成后之后的代码才会继续执行。
可以将以上代码用更喜欢方式重写使用 runBlicking 来包装 main 函数的执行体 fun test3() {runBlockingUnit {GlobalScope.launch {delay(1000)println(World!)}println(Hello )delay(2000)}
}
需要注意是runBlocking 代码块默认运行于其声明所在的线程而 launch 代码块默认运行于线程池中可以通过打印当前线程名来进行区分。
fun test3() {runBlockingUnit {println(Thread.currentThread().name)GlobalScope.launch {println(Thread.currentThread().name)delay(1000)println(World!)}println(Hello )delay(2000)}
} 输出 main // runblocking 运行 main 线程 Hello DefaultDispatcher-worker-1 //GlobalScope 运行线程池 World! 四、job
延迟一段时间等待另一个协程运行并不是一个好的选择可以显式非阻塞的方式地等待协程执行完成
fun test4() {runBlocking {val job GlobalScope.launch {delay(1000)print(World!)}println(Hello )job.join()}
}
现在代码的运行结果仍然是相同但是主协程与后台作业的持续时间没有任何关系这样好多了。
五、launch
Launch 函数是 CoroutineScope 的扩展函数而 runBlocking 的函数体参数也是被声明为 CoroutineScope 的扩展函数所以 launch 函数就隐式持有了和 runBlicking 相同的协程作用域。此时即使 delay 再久 println(World!) 也一定会被执行。
fun test5() {runBlocking {launch {delay(1000)println(World!)}println(Hello )}
}
六、CoroutineScope
除了使用官方的几个协程构建器之外还可以使用 coroutineScope 来声明自己的作用域。 coroutineScope 用于创建一个协程作用域直到所有启动的子协程都完成后才结束。
RunBlocking 和 coroutineScope 看起来很像因为它们都是需要等待其它内部所有相同作用域的子协程结束后才会结束自己。两者的主要区别在于 runBlocking 方法会阻塞当前线程而 coroutineScope 只是挂起并释放底层底层线程以供其他协程使用。由于这个差别所以 runBlocking 是一个普通函数而 coroutineScope 是一个挂起函数。
fun test6(){runBlocking {//非阻塞 GlobalScopelaunch {delay(200)println(Task from runBlocking)}//非阻塞 coroutineScope {launch {delay(500)println(Task from nested launch)}delay(100)println(Task from coroutine scope)}println(Coroutine scope is over)}
} 输出 Task from coroutine scope Task from runBlocking Task from nested launch Coroutine scope is over 六、suspend suspend 挂起函数可以想常规函数一样在协程中使用但是它们的额外特性是可以依次使用其他挂起函数如delay函数来使协程挂起。
fun test7() {runBlocking {//内部调用挂起函数执行耗时任务launch {doWorld()}println(Hello )}
}suspend fun doWorld() {delay(1000)println(World!)
}
七、repeat
协程是轻量级的
fun test8() {runBlocking {//启动 10万个协程repeat(100_000) { i -launch {println(Im sleeping $i ...)delay(500)}}}
}
八、全局协程类似于守护线程
以下代码在 GlobalScope 中启动了一个会长时间运行的协程它每秒打印两次然后延迟一段时间后从 mian 函数返回。
fun test9() {runBlocking {GlobalScope.launch {repeat(1000) { i -println(Im sleeping $i ...)delay(500)}}delay(1300)}
}
输出 Im sleeping 0 ... Im sleeping 1 ... Im sleeping 2 ... 这里由于 launch 函数依附的协程作用域是 GlobalScope而非 runBlocking 所隐含的作用域。在 GlobalScope 中启动的协程无法使进程保持活动状态它们像守护线程当主线程消亡时守护线程也将消亡
八、协程代码块默认顺序执行
fun test10() {runBlocking {val time measureTimeMillis {val one doSomethingUsefulOne()val two doSomethingUsefulTwo()println(The answer is ${one two})}println(Completed in $time ms)}
}suspend fun doSomethingUsefulOne(): Int {delay(1000)return 13
}suspend fun doSomethingUsefulTwo(): Int {delay(1000)return 29
} 输出 The answer is 42 Completed in 2011 ms 九、async
async 实现多个协程异步执行,并可以通过 await() 取出结果返回值launch 类似 async但是无返回值。
fun test11(){runBlocking {val time measureTimeMillis {val one async { doSomethingUsefulOne() }val two async { doSomethingUsefulTwo() }println(The answer is ${one.await() two.await()})}println(Completed in $time ms)}
}
输出 The answer is 42 Completed in 1019 ms 十、lazy
async 可以设置 CoroutineStart.lazy 为懒加载模式。在这情况下 需要 调用 await() 或者 start() 才启动。
fun test12(){runBlocking {val time measureTimeMillis {val one async(start CoroutineStart.LAZY) { doSomethingUsefulOne() }val two async(start CoroutineStart.LAZY) { doSomethingUsefulTwo() }one.start()two.start()println(The answer is ${one.await() two.await()})}println(Completed in $time ms)}
}