blob: 4ebf1269983c91fc2a3a6482acd32a2c59609f66 [file] [log] [blame]
Roman Elizarov8b38fa22017-09-27 17:44:31 +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 Elizarov8b38fa22017-09-27 17:44:31 +03003 */
4
5// 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.context10
Roman Elizarov8b38fa22017-09-27 17:44:31 +03007
8import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03009import kotlin.coroutines.experimental.*
Roman Elizarov8b38fa22017-09-27 17:44:31 +030010
11fun main(args: Array<String>) = runBlocking<Unit> {
12 val job = Job() // create a job object to manage our lifecycle
13 // now launch ten coroutines for a demo, each working for a different time
14 val coroutines = List(10) { i ->
15 // they are all children of our job object
Roman Elizarove8f694e2017-11-28 10:12:00 +030016 launch(coroutineContext, parent = job) { // we use the context of main runBlocking thread, but with our parent job
Roman Elizarov8b38fa22017-09-27 17:44:31 +030017 delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc
18 println("Coroutine $i is done")
19 }
20 }
21 println("Launched ${coroutines.size} coroutines")
22 delay(500L) // delay for half a second
23 println("Cancelling the job!")
24 job.cancelAndJoin() // cancel all our coroutines and wait for all of them to complete
25}