Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
| 18 | package guide.sync.example07 |
| 19 | |
Roman Elizarov | 9669578 | 2017-10-01 10:48:15 -0700 | [diff] [blame] | 20 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9669578 | 2017-10-01 10:48:15 -0700 | [diff] [blame] | 21 | import kotlinx.coroutines.experimental.channels.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 22 | import kotlin.system.* |
| 23 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 24 | |
| 25 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { |
| 26 | val n = 1000 // number of coroutines to launch |
| 27 | val k = 1000 // times an action is repeated by each coroutine |
| 28 | val time = measureTimeMillis { |
| 29 | val jobs = List(n) { |
| 30 | launch(context) { |
| 31 | repeat(k) { action() } |
| 32 | } |
| 33 | } |
| 34 | jobs.forEach { it.join() } |
| 35 | } |
| 36 | println("Completed ${n * k} actions in $time ms") |
| 37 | } |
| 38 | |
| 39 | // Message types for counterActor |
| 40 | sealed class CounterMsg |
| 41 | object IncCounter : CounterMsg() // one-way message to increment counter |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 42 | class GetCounter(val response: CompletableDeferred<Int>) : CounterMsg() // a request with reply |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 43 | |
| 44 | // This function launches a new counter actor |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 45 | fun counterActor() = actor<CounterMsg> { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 46 | var counter = 0 // actor state |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 47 | for (msg in channel) { // iterate over incoming messages |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 48 | when (msg) { |
| 49 | is IncCounter -> counter++ |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 50 | is GetCounter -> msg.response.complete(counter) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 56 | val counter = counterActor() // create the actor |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 57 | massiveRun(CommonPool) { |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 58 | counter.send(IncCounter) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 59 | } |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 60 | // send a message to get a counter value from an actor |
| 61 | val response = CompletableDeferred<Int>() |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 62 | counter.send(GetCounter(response)) |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 63 | println("Counter = ${response.await()}") |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 64 | counter.close() // shutdown the actor |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 65 | } |