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

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.

主站蜘蛛池模板: 麻阳| 青铜峡市| 泽库县| 安多县| 洪湖市| 怀远县| 绍兴县| 北辰区| 通城县| 乌苏市| 和林格尔县| 定西市| 南安市| 巫山县| 黄石市| 紫云| 喀喇沁旗| 正阳县| 拜城县| 太保市| 广河县| 安阳市| 华蓥市| 乐至县| 卓资县| 财经| 泸溪县| 福海县| 通辽市| 桦甸市| 三明市| 贞丰县| 荥阳市| 卓资县| 沁水县| 封丘县| 五寨县| 高陵县| 巴里| 弥勒县| 西昌市|