Skip to content

Commit 075c381

Browse files
committed
Add info about WaitGroup to README.
1 parent 7b7a3e0 commit 075c381

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,21 @@ Channel.select(
315315
ticker.close()
316316
```
317317

318+
#### WaitGroup
319+
Similar to GoLang you can use `WaitGroup` to coordinate multiple threads doing work. Use `add()`, `done()`, and `await()`.
320+
```scala
321+
val wg = WaitGroup()
322+
val n = 10
323+
for (_ <- 1 to n) {
324+
wg.add()
325+
new Thread {
326+
// do some work
327+
wg.done()
328+
}.start()
329+
}
330+
wg.await()
331+
```
332+
318333
### Non-blocking methods
319334
Go supports non-blocking channel operation by the elegant `default` clause in the `select` statement. The scala port
320335
adds separate methods that support non-blocking operations: `trySend()`, `tryRecv()` and `trySelect()`. There is an

src/test/scala/com/github/yruslan/channel/WaitGroupSuite.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ class WaitGroupSuite extends AnyWordSpec {
1111
val n = 10
1212
for (_ <- 1 to n) {
1313
wg.add()
14-
val k = createThread {
14+
createThread {
1515
Thread.sleep(1000)
1616
wg.done()
17-
}
18-
k.start()
17+
}.start()
1918
}
2019
wg.await()
2120
}

0 commit comments

Comments
 (0)