blob: 55c3fa4460fa463dd6c5055bd63f0db455fc5feb [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.sync03
Roman Elizarovf5bc0472017-02-22 11:38:13 +03007
8import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03009import java.util.concurrent.atomic.*
10import kotlin.system.*
11import kotlin.coroutines.experimental.*
Roman Elizarovf5bc0472017-02-22 11:38:13 +030012
Roman Elizarov1e459602017-02-27 11:05:17 +030013suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) {
14 val n = 1000 // number of coroutines to launch
15 val k = 1000 // times an action is repeated by each coroutine
Roman Elizarovf5bc0472017-02-22 11:38:13 +030016 val time = measureTimeMillis {
17 val jobs = List(n) {
Roman Elizarov1e459602017-02-27 11:05:17 +030018 launch(context) {
19 repeat(k) { action() }
Roman Elizarovf5bc0472017-02-22 11:38:13 +030020 }
21 }
22 jobs.forEach { it.join() }
23 }
Roman Elizarov1e459602017-02-27 11:05:17 +030024 println("Completed ${n * k} actions in $time ms")
Roman Elizarovf5bc0472017-02-22 11:38:13 +030025}
26
Roman Elizarov1e459602017-02-27 11:05:17 +030027var counter = AtomicInteger()
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.incrementAndGet()
Roman Elizarovf5bc0472017-02-22 11:38:13 +030032 }
Roman Elizarov1e459602017-02-27 11:05:17 +030033 println("Counter = ${counter.get()}")
Roman Elizarovf5bc0472017-02-22 11:38:13 +030034}