blob: 448add88b73a09addf6e4f55af819236382a9c5c [file] [log] [blame]
Roman Elizarov1e459602017-02-27 11:05:17 +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 Elizarov1e459602017-02-27 11:05:17 +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.sync07
Roman Elizarov1e459602017-02-27 11:05:17 +03007
Roman Elizarov96695782017-10-01 10:48:15 -07008import kotlinx.coroutines.experimental.*
Roman Elizarov96695782017-10-01 10:48:15 -07009import kotlinx.coroutines.experimental.channels.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030010import kotlin.system.*
11import kotlin.coroutines.experimental.*
Roman Elizarov1e459602017-02-27 11:05:17 +030012
13suspend 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
16 val time = measureTimeMillis {
17 val jobs = List(n) {
18 launch(context) {
19 repeat(k) { action() }
20 }
21 }
22 jobs.forEach { it.join() }
23 }
24 println("Completed ${n * k} actions in $time ms")
25}
26
27// Message types for counterActor
28sealed class CounterMsg
29object IncCounter : CounterMsg() // one-way message to increment counter
Roman Elizarov256812a2017-07-22 01:00:30 +030030class GetCounter(val response: CompletableDeferred<Int>) : CounterMsg() // a request with reply
Roman Elizarov1e459602017-02-27 11:05:17 +030031
32// This function launches a new counter actor
Roman Elizarov66f018c2017-09-29 21:39:03 +030033fun counterActor() = actor<CounterMsg> {
Roman Elizarov1e459602017-02-27 11:05:17 +030034 var counter = 0 // actor state
Roman Elizarovc0e19f82017-02-27 11:59:14 +030035 for (msg in channel) { // iterate over incoming messages
Roman Elizarov1e459602017-02-27 11:05:17 +030036 when (msg) {
37 is IncCounter -> counter++
Roman Elizarov256812a2017-07-22 01:00:30 +030038 is GetCounter -> msg.response.complete(counter)
Roman Elizarov1e459602017-02-27 11:05:17 +030039 }
40 }
41}
42
43fun main(args: Array<String>) = runBlocking<Unit> {
Roman Elizarovc0e19f82017-02-27 11:59:14 +030044 val counter = counterActor() // create the actor
Roman Elizarov1e459602017-02-27 11:05:17 +030045 massiveRun(CommonPool) {
Roman Elizarovc0e19f82017-02-27 11:59:14 +030046 counter.send(IncCounter)
Roman Elizarov1e459602017-02-27 11:05:17 +030047 }
Roman Elizarov256812a2017-07-22 01:00:30 +030048 // send a message to get a counter value from an actor
49 val response = CompletableDeferred<Int>()
Roman Elizarovc0e19f82017-02-27 11:59:14 +030050 counter.send(GetCounter(response))
Roman Elizarov256812a2017-07-22 01:00:30 +030051 println("Counter = ${response.await()}")
Roman Elizarovc0e19f82017-02-27 11:59:14 +030052 counter.close() // shutdown the actor
Roman Elizarov1e459602017-02-27 11:05:17 +030053}