blob: abfd0585ff355f913c46584fd3741621a49f85ad [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovf16fd272017-02-07 11:26:00 +03003 */
4
Roman Elizarov2f6d7c92017-02-03 15:16:07 +03005// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
Roman Elizarova9687a32018-06-29 17:28:38 +03006package kotlinx.coroutines.experimental.guide.context07
Roman Elizarov2f6d7c92017-02-03 15:16:07 +03007
8import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03009import kotlin.coroutines.experimental.*
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030010
11fun main(args: Array<String>) = runBlocking<Unit> {
12 // start a coroutine to process some kind of incoming request
Roman Elizarov43e3af72017-07-21 16:01:31 +030013 val request = launch(coroutineContext) { // use the context of `runBlocking`
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030014 // spawns CPU-intensive child job in CommonPool !!!
Roman Elizarov43e3af72017-07-21 16:01:31 +030015 val job = launch(coroutineContext + CommonPool) {
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030016 println("job: I am a child of the request coroutine, but with a different dispatcher")
17 delay(1000)
18 println("job: I will not execute this line if my parent request is cancelled")
19 }
20 job.join() // request completes when its sub-job completes
21 }
22 delay(500)
23 request.cancel() // cancel processing of the request
24 delay(1000) // delay a second to see what happens
25 println("main: Who has survived request cancellation?")
26}