官术网_书友最值得收藏!

Active

A job in the state new can be started in many ways, but commonly it will be done by calling start() or join(), the difference being that the former will start the job without waiting for it to complete, whereas the latter will suspend execution until the job completes. Take this example into consideration:

fun main(args: Array<String>) {
val job = launch(start = CoroutineStart.LAZY) {
delay(3000)
}

job.start()
}

The mentioned code will not suspend execution when job.start() is invoked, so the application will complete its execution without waiting for job to complete.

Since start() doesn't suspend execution, it doesn't need to be called from a suspending function or coroutine. It can be called from any part of our application.

If we use join(),we will force the application to wait for job to complete, as demonstrated:

fun main(args: Array<String>) = runBlocking {
val job = launch(start = CoroutineStart.LAZY) {
delay(3000)
}

job.join()
}
As  join() can suspend execution, it needs to be called from a coroutine or a suspending function. Note that runBlocking() is being used for that.

Any job that has been started is therefore active, and it will be active until it completes execution or until cancellation is requested.

主站蜘蛛池模板: 东明县| 灯塔市| 泰州市| 淳安县| 河南省| 鄂尔多斯市| 兴化市| 凌海市| 新竹县| 行唐县| 石阡县| 随州市| 邓州市| 兰考县| 霞浦县| 石家庄市| 曲周县| 固原市| 彭山县| 虎林市| 安岳县| 广河县| 伊川县| 大渡口区| 屯留县| 九江县| 西宁市| 集贤县| 商水县| 卓资县| 河源市| 内丘县| 公安县| 余庆县| 青岛市| 大余县| 资兴市| 海门市| 丰都县| 隆昌县| 垫江县|