| /* |
| * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| */ |
| |
| // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
| package kotlinx.coroutines.experimental.guide.sync01b |
| |
| import kotlinx.coroutines.experimental.* |
| import kotlin.system.* |
| import kotlin.coroutines.experimental.* |
| |
| suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { |
| val n = 1000 // number of coroutines to launch |
| val k = 1000 // times an action is repeated by each coroutine |
| val time = measureTimeMillis { |
| val jobs = List(n) { |
| launch(context) { |
| repeat(k) { action() } |
| } |
| } |
| jobs.forEach { it.join() } |
| } |
| println("Completed ${n * k} actions in $time ms") |
| } |
| |
| val mtContext = newFixedThreadPoolContext(2, "mtPool") // explicitly define context with two threads |
| var counter = 0 |
| |
| fun main(args: Array<String>) = runBlocking<Unit> { |
| massiveRun(mtContext) { // use it instead of CommonPool in this sample and below |
| counter++ |
| } |
| println("Counter = $counter") |
| } |