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

网站建设项目明细表加强公司网站平台建设的意义

网站建设项目明细表,加强公司网站平台建设的意义,cms是什么软件,邯郸网上房地产官网一、什么叫协程 协程可以称为轻量级线程,线程代码块; 二、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.yayakq.cn/news/434934/

相关文章:

  • 佛山网站排名提升wordpress 设置访客登陆
  • 做编程的网站有哪些群晖非插件搭建wordpress
  • 建设和谐社区网站怎么做网站图片seo
  • 怎样加入装修接单网站网站开发常用中间件
  • 东莞市建设小学网站中华建筑网
  • 网站开发项目方案网站编辑转行做文案
  • 购物网站需求分析报告营销管理制度范本
  • 关于旅游的网站建设目的网络游戏排行榜前十名大型网络游戏
  • 网站空间不能读数据库网站设计公司哪家比较好
  • 公司网站彩页怎么做智联招聘企业登录入口
  • 虚拟主机网站淘客网站建设入侵WordPress网站
  • 福州专业做网站的公司哪家好做文案用什么网站
  • 企业网站做推广大型的seo公司
  • 平台式建站企业用的邮箱是什么邮箱
  • 没有域名 怎么做网站链接深圳网站设计排名
  • 可以做蛋白三位结构图的网站一个网站两个域名
  • 网站后台和前台不一致个人网站毕业设计
  • 沧州网站建设开发服务手机电商网站 模板
  • 如何建立竞价网站WordPress显示403
  • 网站建设开发协议网站建设技术服务的方式是什么
  • 受欢迎的广州网站设计投稿网站
  • 专业定制网站建设wordpress启动慢
  • 重庆永川微网站建设网站建设一般报价多少钱
  • 动易后台 网站统计调查 报表类型怎样使用新闻发稿
  • 东西湖建设局网站如何在网上销售
  • 电商网站设计 页面转化率html编辑软件
  • 专业做企业活动的趴网站泰安企业网站建设公司
  • 管理系统和网站哪个好做移动微网站开发
  • 自己做一个网站需要多少钱公司建网站怎么弄
  • 公司网站建设代理一般做多久wordpress按钮代码