Skip to content

Commit 15d2d84

Browse files
Updated code samples.
1 parent ba0b7dc commit 15d2d84

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

‎chapter7/patterns/pool/main/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func main() {
7373
wg.Wait()
7474

7575
// Close the pool.
76-
fmt.Println("*****> Shutdown Program.")
76+
fmt.Println("Shutdown Program.")
7777
p.Close()
7878
}
7979

‎chapter7/patterns/work/main/main.go‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package main
88

99
import (
1010
"fmt"
11+
"sync"
1112
"time"
1213

1314
"github.com/goinaction/code/chapter7/patterns/work"
@@ -38,19 +39,29 @@ func main() {
3839
// Create a work value with 2 goroutines.
3940
w := work.New(2)
4041

41-
// Iterate over the slice of names.
42-
for _, name := range names {
43-
// Create a namePrinter and provide the
44-
// specfic name.
45-
np := namePrinter{
46-
name: name,
47-
}
42+
var wg sync.WaitGroup
43+
wg.Add(10 * len(names))
44+
45+
for i := 0; i < 10; i++ {
46+
// Iterate over the slice of names.
47+
for _, name := range names {
48+
// Create a namePrinter and provide the
49+
// specfic name.
50+
np := namePrinter{
51+
name: name,
52+
}
4853

49-
// Submit the task to be worked on. When RunTask
50-
// returns we know it is being handled.
51-
w.RunTask(&np)
54+
go func() {
55+
// Submit the task to be worked on. When RunTask
56+
// returns we know it is being handled.
57+
w.RunTask(&np)
58+
wg.Done()
59+
}()
60+
}
5261
}
5362

63+
wg.Wait()
64+
5465
// Shutdown the work and wait for all existing work
5566
// to be completed.
5667
w.Shutdown()

0 commit comments

Comments
 (0)