blob: 268a4e9cc3483e17ba5b3142af00fb727438b215 [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 Elizarovb3d55a52017-02-03 12:47:21 +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.cancel05
Roman Elizarov1293ccd2017-02-01 18:49:54 +03007
8import kotlinx.coroutines.experimental.*
9
10fun main(args: Array<String>) = runBlocking<Unit> {
Roman Elizarov66f018c2017-09-29 21:39:03 +030011 val job = launch {
Roman Elizarov1293ccd2017-02-01 18:49:54 +030012 try {
13 repeat(1000) { i ->
14 println("I'm sleeping $i ...")
15 delay(500L)
16 }
17 } finally {
Roman Elizarovf9e13f52017-12-21 12:23:15 +030018 withContext(NonCancellable) {
Roman Elizarov1293ccd2017-02-01 18:49:54 +030019 println("I'm running finally")
20 delay(1000L)
21 println("And I've just delayed for 1 sec because I'm non-cancellable")
22 }
23 }
24 }
25 delay(1300L) // delay a bit
26 println("main: I'm tired of waiting!")
Roman Elizarov8b38fa22017-09-27 17:44:31 +030027 job.cancelAndJoin() // cancels the job and waits for its completion
Roman Elizarov1293ccd2017-02-01 18:49:54 +030028 println("main: Now I can quit.")
Roman Elizarovb3d55a52017-02-03 12:47:21 +030029}