blob: 04b49f4dfd2469ee271c7e21859374c0adff3f14 [file] [log] [blame]
Roman Elizarovf5bc0472017-02-22 11:38:13 +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 Elizarovf5bc0472017-02-22 11:38:13 +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.sync02
Roman Elizarovf5bc0472017-02-22 11:38:13 +03007
8import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03009import kotlin.system.*
10import kotlin.coroutines.experimental.*
Roman Elizarovf5bc0472017-02-22 11:38:13 +030011
Roman Elizarov1e459602017-02-27 11:05:17 +030012suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) {
13 val n = 1000 // number of coroutines to launch
14 val k = 1000 // times an action is repeated by each coroutine
Roman Elizarovf5bc0472017-02-22 11:38:13 +030015 val time = measureTimeMillis {
16 val jobs = List(n) {
Roman Elizarov1e459602017-02-27 11:05:17 +030017 launch(context) {
18 repeat(k) { action() }
Roman Elizarovf5bc0472017-02-22 11:38:13 +030019 }
20 }
21 jobs.forEach { it.join() }
22 }
Roman Elizarov1e459602017-02-27 11:05:17 +030023 println("Completed ${n * k} actions in $time ms")
Roman Elizarovf5bc0472017-02-22 11:38:13 +030024}
25
Roman Elizarov1e459602017-02-27 11:05:17 +030026@Volatile // in Kotlin `volatile` is an annotation
27var counter = 0
Roman Elizarovf5bc0472017-02-22 11:38:13 +030028
29fun main(args: Array<String>) = runBlocking<Unit> {
Roman Elizarov1e459602017-02-27 11:05:17 +030030 massiveRun(CommonPool) {
31 counter++
Roman Elizarovf5bc0472017-02-22 11:38:13 +030032 }
Roman Elizarov1e459602017-02-27 11:05:17 +030033 println("Counter = $counter")
Roman Elizarovf5bc0472017-02-22 11:38:13 +030034}