blob: d1a5ee5cb4ce8352141f4923dbf7475d631af384 [file] [log] [blame]
Roman Elizarov43e90112017-05-10 11:25:20 +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 Elizarov43e90112017-05-10 11:25:20 +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.sync01b
Roman Elizarov43e90112017-05-10 11:25:20 +03007
8import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03009import kotlin.system.*
10import kotlin.coroutines.experimental.*
Roman Elizarov43e90112017-05-10 11:25:20 +030011
12suspend 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
15 val time = measureTimeMillis {
16 val jobs = List(n) {
17 launch(context) {
18 repeat(k) { action() }
19 }
20 }
21 jobs.forEach { it.join() }
22 }
23 println("Completed ${n * k} actions in $time ms")
24}
25
26val mtContext = newFixedThreadPoolContext(2, "mtPool") // explicitly define context with two threads
27var counter = 0
28
29fun main(args: Array<String>) = runBlocking<Unit> {
30 massiveRun(mtContext) { // use it instead of CommonPool in this sample and below
31 counter++
32 }
33 println("Counter = $counter")
34}