1
1
# Fork & join threads
2
2
3
- It's safest to use higher-level methods, such as ` par ` or ` raceSuccess ` , however this isn't always sufficient. For
3
+ It's safest to use higher-level methods, such as ` par ` or ` race ` , however this isn't always sufficient. For
4
4
these cases, threads can be started using the structured concurrency APIs described below.
5
5
6
- Forks (new threads) can only be started with a ** scope** . Such a scope is defined using the ` supervised ` or ` scoped `
7
- methods.
6
+ Forks (new threads) can only be started within a ** concurrency scope** . Such a scope is defined using the ` supervised ` ,
7
+ ` supervisedError ` or ` scoped ` methods.
8
8
9
9
The lifetime of the forks is defined by the structure of the code, and corresponds to the enclosing ` supervised ` or
10
10
` scoped ` block. Once the code block passed to the scope completes, any forks that are still running are interrupted.
@@ -13,10 +13,11 @@ The whole block will complete only once all forks have completed (successfully,
13
13
Hence, it is guaranteed that all forks started within ` supervised ` or ` scoped ` will finish successfully, with an
14
14
exception, or due to an interrupt.
15
15
16
+ For example, the code below is equivalent to ` par ` :
17
+
16
18
``` scala
17
19
import ox .{fork , supervised }
18
20
19
- // same as `par`
20
21
supervised {
21
22
val f1 = fork {
22
23
Thread .sleep(2000 )
@@ -59,8 +60,8 @@ The default scope, created with `supervised`, watches over the forks that are st
59
60
60
61
This means that the scope will end only when either:
61
62
62
- * all (user, supervised) forks, including the main body passed to ` supervised ` , succeed
63
- * or any (supervised) fork, including the main body passed to ` supervised ` , fails
63
+ * all (user, supervised) forks, including the body passed to ` supervised ` , succeed
64
+ * or any (supervised) fork, including the body passed to ` supervised ` , fails
64
65
65
66
Hence an exception in any of the forks will cause the whole scope to end. Ending the scope means that all running forks
66
67
are cancelled (using interruption). Once all forks complete, the exception is propagated further, that is re-thrown by
@@ -85,14 +86,14 @@ supervised {
85
86
86
87
## User, daemon and unsupervised forks
87
88
88
- In supervised scoped , forks created using ` fork ` behave as daemon threads. That is, their failure ends the scope, but
89
- the scope will also end once the main body and all user forks succeed, regardless if the (daemon) fork is still running.
89
+ In supervised scopes , forks created using ` fork ` behave as daemon threads. That is, their failure ends the scope, but
90
+ the scope will also end once the body and all user forks succeed, regardless if the (daemon) fork is still running.
90
91
91
92
Alternatively, a user fork can be created using ` forkUser ` . Such a fork is required to complete successfully, in order
92
- for the scope to end successfully. Hence when the main body of the scope completes, the scope will wait until all user
93
+ for the scope to end successfully. Hence, when the body of the scope completes, the scope will wait until all user
93
94
forks have completed as well.
94
95
95
- Finally, entirely unsupervised forks can be ran using ` forkUnsupervised ` .
96
+ Finally, entirely unsupervised forks can be started using ` forkUnsupervised ` .
96
97
97
98
## Unsupervised scopes
98
99
@@ -115,11 +116,3 @@ it involves creating a nested scope and two virtual threads, instead of one.
115
116
The ` CancellableFork ` trait exposes the ` .cancel ` method, which interrupts the fork and awaits its completion.
116
117
Alternatively, ` .cancelNow ` returns immediately. In any case, the enclosing scope will only complete once all forks have
117
118
completed.
118
-
119
- ## Error handling
120
-
121
- In supervised mode, if a fork fails with an exception, the enclosing scope will end.
122
-
123
- Moreover, if a fork fails with an exception, the ` Fork.join ` method will throw that exception.
124
-
125
- In unsupervised mode, if there's no join and the fork fails, the exception might go unnoticed.
0 commit comments