Skip to content

Commit dae510b

Browse files
paolopqwwdfsad
authored andcommitted
Update coroutines-guide.md
- Some integrations to the Actor section - Adjust text/comment style - Rewording
1 parent 29bfa1e commit dae510b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

coroutines-guide.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,9 +1688,9 @@ Now let's see how it works in practice:
16881688
fun main(args: Array<String>) = runBlocking<Unit> {
16891689
val tickerChannel = ticker(delay = 100, initialDelay = 0) // create ticker channel
16901690
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
1691-
println("Initial element is available immediately: $nextElement") // Initial delay hasn't passed yet
1691+
println("Initial element is available immediately: $nextElement") // initial delay hasn't passed yet
16921692

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

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

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

20282028
### Actors
20292029

2030-
An actor is a combination of a coroutine, the state that is confined and is encapsulated into this coroutine,
2030+
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,
20312031
and a channel to communicate with other coroutines. A simple actor can be written as a function,
20322032
but an actor with a complex state is better suited for a class.
20332033

@@ -2089,7 +2089,7 @@ Counter = 1000000
20892089

20902090
It does not matter (for correctness) what context the actor itself is executed in. An actor is
20912091
a coroutine and a coroutine is executed sequentially, so confinement of the state to the specific coroutine
2092-
works as a solution to the problem of shared mutable state.
2092+
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).
20932093

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

21862186
### Selecting on close
21872187

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

0 commit comments

Comments
 (0)