Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
Roman Elizarov | a9687a3 | 2018-06-29 17:28:38 +0300 | [diff] [blame] | 6 | package kotlinx.coroutines.experimental.guide.sync07 |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 7 | |
Roman Elizarov | 9669578 | 2017-10-01 10:48:15 -0700 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9669578 | 2017-10-01 10:48:15 -0700 | [diff] [blame] | 9 | import kotlinx.coroutines.experimental.channels.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 10 | import kotlin.system.* |
| 11 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 12 | |
| 13 | suspend 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 |
| 28 | sealed class CounterMsg |
| 29 | object IncCounter : CounterMsg() // one-way message to increment counter |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 30 | class GetCounter(val response: CompletableDeferred<Int>) : CounterMsg() // a request with reply |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 31 | |
| 32 | // This function launches a new counter actor |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 33 | fun counterActor() = actor<CounterMsg> { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 34 | var counter = 0 // actor state |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 35 | for (msg in channel) { // iterate over incoming messages |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 36 | when (msg) { |
| 37 | is IncCounter -> counter++ |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 38 | is GetCounter -> msg.response.complete(counter) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 44 | val counter = counterActor() // create the actor |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 45 | massiveRun(CommonPool) { |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 46 | counter.send(IncCounter) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 47 | } |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 48 | // send a message to get a counter value from an actor |
| 49 | val response = CompletableDeferred<Int>() |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 50 | counter.send(GetCounter(response)) |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 51 | println("Counter = ${response.await()}") |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 52 | counter.close() // shutdown the actor |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 53 | } |