当前位置: 首页 > news >正文

海淀网站建设公司全网推广

海淀网站建设,公司全网推广,物流网站建设,北京市公安局网站备案一、什么叫协程 协程可以称为轻量级线程,线程代码块; 二、GlobalScope 协程 CoroutineScope (协程作用域) 的上下文中通过 launch、async 等构造器来启动。GlobalScope ,即全局作用域内启动了一个新的协程,这意味这该协程的生命周期只受整…

一、什么叫协程

协程可以称为轻量级线程,线程代码块;

二、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() {runBlocking<Unit> {GlobalScope.launch {delay(1000)println("World!")}println("Hello ")delay(2000)}
}

需要注意是,runBlocking 代码块默认运行于其声明所在的线程,而 launch 代码块默认运行于线程池中,可以通过打印当前线程名来进行区分。

fun test3() {runBlocking<Unit> {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("I'm sleeping $i ...")delay(500)}}}
}

八、全局协程类似于守护线程

以下代码在 GlobalScope 中启动了一个会长时间运行的协程,它每秒打印两次,然后延迟一段时间后从 mian 函数返回。

fun test9() {runBlocking {GlobalScope.launch {repeat(1000) { i ->println("I'm sleeping $i ...")delay(500)}}delay(1300)}
}

输出:

I'm sleeping 0 ...
I'm sleeping 1 ...
I'm 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")}
}

http://www.hkea.cn/news/762566/

相关文章:

  • 做网站被拘留免费找客源软件
  • 门户型网站建设百度seo快速提升排名
  • 印度做杂质的网站如何进行网络推广
  • 建设厅八大员兴安盟新百度县seo快速排名
  • 南京网站建设索q.479185700小说排行榜百度
  • 幼儿做爰网站seo工程师是什么职业
  • 申请空间 建立网站吗西安百度推广运营
  • 做花馍网站百度联盟
  • 沈阳建设企业网站google浏览器官网
  • 毕业论文 网站开发营销qq下载
  • 建网站要多长时间外贸网站优化
  • 苹果网站做的好的点电脑培训网上免费课程
  • 做网站开源互联网优化是什么意思
  • 模仿做网站b站上海热点新闻
  • phpcmsv9网站地图地推的60种方法
  • 湖南手机版建站系统哪个好百度网盘app怎么打开链接
  • asp网站开发的实训报告电商营销推广有哪些?
  • 交互设计流程外贸网站优化公司
  • 网络营销网站策划个人网站seo入门
  • 云南省网站备案要求全渠道营销的概念
  • 装修网站合作平台有哪些torrentkitty磁力猫
  • 大理网站开发长春seo结算
  • 西双版纳疫情最新情况seo营销的概念
  • 网站推广seo蜘蛛屯优化排名模板建网站价格
  • 怎样建设公司网站小程序b2b网站大全免费
  • 百度抓取不到网站百度识图搜索图片来源
  • 企业网站栏目规划的重要性网络营销的概述
  • 公司网站建设找谁做免费发布推广信息网站
  • 虚拟币网站开发seo百度关键字优化
  • 网站建设都 包括哪些淄博网站制作