File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ package main
88
99import (
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 ()
You can’t perform that action at this time.
0 commit comments