网站建设企业谁家好,asp网站建设项目实训,建筑设计文字说明,网站背景Kotlin协程 背景定义实践GlobalScope.launchrunBlocking业务实践 背景
在项目实践过程中#xff0c;笔者发现很多异步或者耗时的操作#xff0c;都使用了Kotlin中的协程#xff0c;所以特地研究了一番。
定义
关于协程#xff08;Coroutine#xff09;#xff0c;其实… Kotlin协程 背景定义实践GlobalScope.launchrunBlocking业务实践 背景
在项目实践过程中笔者发现很多异步或者耗时的操作都使用了Kotlin中的协程所以特地研究了一番。
定义
关于协程Coroutine其实更多的应该是一种并发设计模式。与传统的线程或进程相比协程更加轻量级因为它运行在线程之上在执行过程中可以主动地暂停并在需要时恢复执行。而且这些切换操作是在操作系统中的用户态完成的。
实践
引入maven依赖
dependencygroupIdorg.jetbrains.kotlinx/groupIdartifactIdkotlinx-coroutines-core/artifactId
/dependency两种启动模式 runBlockingGlobalScope.launch
GlobalScope.launch
在 GlobalScope.launch 中启动的协程不会阻塞主线程。主线程会继续执行而协程会在后台执行。在上述例子中为了演示效果使用了 Thread.sleep(2000) 来模拟等待协程执行。 其中launch 是一个函数用于创建协程并将其函数主体的执行分派给相应的调度程序。
fun main() {// 启动协程GlobalScope.launch {delay(2000) // 挂起协程模拟异步操作println(World!)}println(Hello,)Thread.sleep(4000) // 阻塞主线程等待协程执行完毕
}runBlocking
在 runBlocking 中启动的协程会阻塞当前线程直到所有启动的协程都执行完毕。
fun main() runBlocking {// 在 runBlocking 作用域内启动协程launch {// 协程代码println(Inside coroutine)}// 主线程继续执行println(Outside coroutine)// 阻塞主线程等待协程执行完毕
}业务实践
Dispatchers.IO Kotlin提供的协程调度方式指示此协程应在为 I/O 操作预留的线程上执行适合在主线程之外执行磁盘或网络 I/O。Dispatchers.Main 使用此调度程序可在主线程上运行协程。在官方文档介绍中主要用于安卓开发中与界面交互和执行快速工作。Dispatchers.Default 适合在主线程之外执行占用大量 CPU 资源的工作。
private fun sendRefundHttpPost(weChatPayRefundUrl: String, xmlRequestData: String): MapString, String runBlocking {// 启用协程发送退款操作returnrunBlocking withContext(Dispatchers.IO) {val responseXmlStr careWeChatHttps.postXmlForString(weChatPayRefundUrl, xmlRequestData)WXPayUtil.xmlToMap(responseXmlStr)}}private fun getSchoolDate(allTeacherIds: SetString): WildSchoolData runBlocking {// 涉及统计的allTeacherIds 数量比较大, 创建异步任务去操作val elements allTeacherIds.chunked(60).map {async(Dispatchers.Default) {val totalCount teacherPerformanceServiceApi.getTeachersTotalPerformanceCount(it).performanceCountval currentWeekCount teacherPerformanceServiceApi.getTeachersCurrentWeekPerformanceCountByByRangeTime(it).performanceCountWildSchoolData(totalCount, currentWeekCount)}}// 调用await等待异步任务结束returnrunBlocking WildSchoolData(elements.awaitAll().sumBy { it.totalCount }, elements.awaitAll().sumBy { it.currentWeekCount })}fun savePoint(pagePointVo: PagePointVo): Any {// 启用协程执行一个操作流水的插入, 不影响其他操作GlobalScope.launch(context Dispatchers.IO) {val pagePoint PagePoint().apply {this.schoolId pagePointVo.schoolIdthis.userId pagePointVo.userIdthis.userRole pagePointVo.userRolethis.moduleKey pagePointVo.moduleKeythis.eventId pagePointVo.eventIdthis.clickTime Date()}pagePointMapper.insert(pagePoint)}// 其他操作}
文章转载自: http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.fprll.cn.gov.cn.fprll.cn http://www.morning.ngjpt.cn.gov.cn.ngjpt.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn