Addressing review comments
* Renamed the example * Reworded comments * Moved it to after worker pools * Sleep for a second instead of random * Mention the new sample in worker pools
This commit is contained in:
parent
6ab81bdf71
commit
74ca2a7b0f
@ -23,7 +23,6 @@ Goroutines
|
|||||||
Channels
|
Channels
|
||||||
Channel Buffering
|
Channel Buffering
|
||||||
Channel Synchronization
|
Channel Synchronization
|
||||||
Waiting For Goroutines To Finish
|
|
||||||
Channel Directions
|
Channel Directions
|
||||||
Select
|
Select
|
||||||
Timeouts
|
Timeouts
|
||||||
@ -33,6 +32,7 @@ Range over Channels
|
|||||||
Timers
|
Timers
|
||||||
Tickers
|
Tickers
|
||||||
Worker Pools
|
Worker Pools
|
||||||
|
WaitGroup
|
||||||
Rate Limiting
|
Rate Limiting
|
||||||
Atomic Counters
|
Atomic Counters
|
||||||
Mutexes
|
Mutexes
|
||||||
|
@ -1,27 +1,22 @@
|
|||||||
// To wait for multiple goroutines to finish, we can
|
// To wait for multiple goroutines to finish, we can
|
||||||
// use a sync.WaitGroup.
|
// use a *wait group*.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This is the function we'll run in every goroutine.
|
// This is the function we'll run in every goroutine.
|
||||||
// wg is the WaitGroup it uses to notify that it's done.
|
|
||||||
// Note that a WaitGroup must be passed to functions by
|
// Note that a WaitGroup must be passed to functions by
|
||||||
// pointer.
|
// pointer.
|
||||||
func worker(id int, wg *sync.WaitGroup) {
|
func worker(id int, wg *sync.WaitGroup) {
|
||||||
fmt.Printf("Worker %d starting\n", id)
|
fmt.Printf("Worker %d starting\n", id)
|
||||||
|
|
||||||
// Sleep for a random duration between 500-700 ms
|
// Sleep to simulate an expensive task.
|
||||||
// to simulate work. See the [random numbers](random-numbers)
|
time.Sleep(time.Second)
|
||||||
// example for more details on *rand*.
|
|
||||||
msToSleep := time.Duration(500 + rand.Intn(200))
|
|
||||||
time.Sleep(msToSleep * time.Millisecond)
|
|
||||||
fmt.Printf("Worker %d done\n", id)
|
fmt.Printf("Worker %d done\n", id)
|
||||||
|
|
||||||
// Notify the WaitGroup that we're done.
|
// Notify the WaitGroup that we're done.
|
2
examples/waitgroup/waitgroup.hash
Normal file
2
examples/waitgroup/waitgroup.hash
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ffc6520e73ebfa2b8c470e3ef00fee55388234e0
|
||||||
|
8cD2V9CgI0J
|
@ -1,4 +1,4 @@
|
|||||||
$ go run waiting-for-goroutines-to-finish.go
|
$ go run waitgroup.go
|
||||||
Worker 5 starting
|
Worker 5 starting
|
||||||
Worker 3 starting
|
Worker 3 starting
|
||||||
Worker 4 starting
|
Worker 4 starting
|
@ -1,2 +0,0 @@
|
|||||||
f068072d11ed9469174c18f5b7a6a7d9d8d3dafb
|
|
||||||
koKzXfbq8kg
|
|
@ -42,6 +42,9 @@ func main() {
|
|||||||
close(jobs)
|
close(jobs)
|
||||||
|
|
||||||
// Finally we collect all the results of the work.
|
// Finally we collect all the results of the work.
|
||||||
|
// This also ensures that the worker goroutines have
|
||||||
|
// finished. An alternative way to wait for multiple
|
||||||
|
// goroutines is to use a [WaitGroup](waitgroup).
|
||||||
for a := 1; a <= 5; a++ {
|
for a := 1; a <= 5; a++ {
|
||||||
<-results
|
<-results
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
1f9acf1e50be05cad73e6b085ed3294892c67d42
|
bfd2824b3840ff67fa9a0218c7be66647b4bf3d9
|
||||||
RTRcHA05vV
|
IQestAFcxLh
|
||||||
|
@ -171,7 +171,7 @@ started.</p>
|
|||||||
|
|
||||||
|
|
||||||
<p class="next">
|
<p class="next">
|
||||||
Next example: <a href="waiting-for-goroutines-to-finish">Waiting For Goroutines To Finish</a>.
|
Next example: <a href="channel-directions">Channel Directions</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="footer">
|
<p class="footer">
|
||||||
|
@ -85,8 +85,6 @@
|
|||||||
|
|
||||||
<li><a href="channel-synchronization">Channel Synchronization</a></li>
|
<li><a href="channel-synchronization">Channel Synchronization</a></li>
|
||||||
|
|
||||||
<li><a href="waiting-for-goroutines-to-finish">Waiting For Goroutines To Finish</a></li>
|
|
||||||
|
|
||||||
<li><a href="channel-directions">Channel Directions</a></li>
|
<li><a href="channel-directions">Channel Directions</a></li>
|
||||||
|
|
||||||
<li><a href="select">Select</a></li>
|
<li><a href="select">Select</a></li>
|
||||||
@ -105,6 +103,8 @@
|
|||||||
|
|
||||||
<li><a href="worker-pools">Worker Pools</a></li>
|
<li><a href="worker-pools">Worker Pools</a></li>
|
||||||
|
|
||||||
|
<li><a href="waitgroup">WaitGroup</a></li>
|
||||||
|
|
||||||
<li><a href="rate-limiting">Rate Limiting</a></li>
|
<li><a href="rate-limiting">Rate Limiting</a></li>
|
||||||
|
|
||||||
<li><a href="atomic-counters">Atomic Counters</a></li>
|
<li><a href="atomic-counters">Atomic Counters</a></li>
|
||||||
|
@ -40,7 +40,7 @@ a <em>worker pool</em> using goroutines and channels.</p>
|
|||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code leading">
|
<td class="code leading">
|
||||||
<a href="http://play.golang.org/p/RTRcHA05vV"><img title="Run code" src="play.png" class="run" /></a>
|
<a href="http://play.golang.org/p/IQestAFcxLh"><img title="Run code" src="play.png" class="run" /></a>
|
||||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
@ -147,7 +147,11 @@ channel to indicate that’s all the work we have.</p>
|
|||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td class="docs">
|
<td class="docs">
|
||||||
<p>Finally we collect all the results of the work.</p>
|
<p>Finally we collect all the results of the work.
|
||||||
|
This also ensures that the worker goroutines have
|
||||||
|
finished. An alternative way to wait for multiple
|
||||||
|
goroutines that’s useful in many scenarios is
|
||||||
|
to use a <a href="waitgroup">WaitGroup</a>.</p>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code">
|
<td class="code">
|
||||||
@ -207,7 +211,7 @@ there are 3 workers operating concurrently.</p>
|
|||||||
|
|
||||||
|
|
||||||
<p class="next">
|
<p class="next">
|
||||||
Next example: <a href="rate-limiting">Rate Limiting</a>.
|
Next example: <a href="waitgroup">WaitGroup</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="footer">
|
<p class="footer">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user