Skip to content

Commit 1d6e493

Browse files
paolopelizarov
authored andcommitted
Update coroutines-guide.md
- Some integrations to the Actor section - Adjust text/comment style - Rewording
1 parent 2a63775 commit 1d6e493

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

coroutines-guide.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1676,9 +1676,9 @@ Now let's see how it works in practice:
16761676
fun main(args: Array<String>) = runBlocking<Unit> {
16771677
val tickerChannel = ticker(delay = 100, initialDelay = 0) // create ticker channel
16781678
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
1679-
println("Initial element is available immediately: $nextElement") // Initial delay hasn't passed yet
1679+
println("Initial element is available immediately: $nextElement") // initial delay hasn't passed yet
16801680

1681-
nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // All subsequent elements has 100ms delay
1681+
nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // all subsequent elements has 100ms delay
16821682
println("Next element is not ready in 50 ms: $nextElement")
16831683

16841684
nextElement = withTimeoutOrNull(60) { tickerChannel.receive() }
@@ -1983,7 +1983,7 @@ This now works much faster and produces correct result.
19831983
Mutual exclusion solution to the problem is to protect all modifications of the shared state with a _critical section_
19841984
that is never executed concurrently. In a blocking world you'd typically use `synchronized` or `ReentrantLock` for that.
19851985
Coroutine's alternative is called [Mutex]. It has [lock][Mutex.lock] and [unlock][Mutex.unlock] functions to
1986-
delimit a critical section. The key difference is that `Mutex.lock` is a suspending function. It does not block a thread.
1986+
delimit a critical section. The key difference is that `Mutex.lock()` is a suspending function. It does not block a thread.
19871987

19881988
There is also [withLock] extension function that conveniently represents
19891989
`mutex.lock(); try { ... } finally { mutex.unlock() }` pattern:
@@ -2015,7 +2015,7 @@ is confined to.
20152015

20162016
### Actors
20172017

2018-
An actor is a combination of a coroutine, the state that is confined and is encapsulated into this coroutine,
2018+
An [actor](https://en.wikipedia.org/wiki/Actor_model) is an entity made up of a combination of a coroutine, the state that is confined and encapsulated into this coroutine,
20192019
and a channel to communicate with other coroutines. A simple actor can be written as a function,
20202020
but an actor with a complex state is better suited for a class.
20212021

@@ -2077,7 +2077,7 @@ Counter = 1000000
20772077

20782078
It does not matter (for correctness) what context the actor itself is executed in. An actor is
20792079
a coroutine and a coroutine is executed sequentially, so confinement of the state to the specific coroutine
2080-
works as a solution to the problem of shared mutable state.
2080+
works as a solution to the problem of shared mutable state. Indeed, actors may modify their own private state, but can only affect each other through messages (avoiding the need for any locks).
20812081

20822082
Actor is more efficient than locking under load, because in this case it always has work to do and it does not
20832083
have to switch to a different context at all.
@@ -2173,8 +2173,8 @@ buzz -> 'Buzz!'
21732173

21742174
### Selecting on close
21752175

2176-
The [onReceive][ReceiveChannel.onReceive] clause in `select` fails when the channel is closed and the corresponding
2177-
`select` throws an exception. We can use [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] clause to perform a
2176+
The [onReceive][ReceiveChannel.onReceive] clause in `select` fails when the channel is closed causing the corresponding
2177+
`select` to throw an exception. We can use [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] clause to perform a
21782178
specific action when the channel is closed. The following example also shows that `select` is an expression that returns
21792179
the result of its selected clause:
21802180

0 commit comments

Comments
 (0)