blob: b37b06b85c6a587d8909c1ed827f1e64e318e5c5 [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +03001/*
Roman Elizarovdb0ef0c2019-07-03 15:02:44 +03002 * Copyright 2016-2019 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 Elizarov0950dfa2018-07-13 10:33:25 +03006package kotlinx.coroutines.guide.context06
Roman Elizarov2f6d7c92017-02-03 15:16:07 +03007
Roman Elizarov0950dfa2018-07-13 10:33:25 +03008import kotlinx.coroutines.*
Roman Elizarov2f6d7c92017-02-03 15:16:07 +03009
Prendota65e6c8c2018-10-17 11:51:08 +030010fun main() = runBlocking<Unit> {
Roman Elizarov66f018c2017-09-29 21:39:03 +030011 // launch a coroutine to process some kind of incoming request
Roman Elizarovc32579e2018-09-09 19:21:43 +030012 val request = launch {
13 // it spawns two other jobs, one with GlobalScope
14 GlobalScope.launch {
15 println("job1: I run in GlobalScope and execute independently!")
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030016 delay(1000)
17 println("job1: I am not affected by cancellation of the request")
18 }
19 // and the other inherits the parent context
Roman Elizarovc32579e2018-09-09 19:21:43 +030020 launch {
Roman Elizarov74619c12017-11-09 10:32:15 +030021 delay(100)
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030022 println("job2: I am a child of the request coroutine")
23 delay(1000)
24 println("job2: I will not execute this line if my parent request is cancelled")
25 }
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030026 }
27 delay(500)
28 request.cancel() // cancel processing of the request
29 delay(1000) // delay a second to see what happens
30 println("main: Who has survived request cancellation?")
Roman Elizarov2f6d7c92017-02-03 15:16:07 +030031}