mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-29 08:26:04 +03:00
Added sync wait group
This commit is contained in:
parent
57c0d5dff8
commit
e93498f9f9
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,7 +12,8 @@ type Job struct {
|
|||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputText(j *Job) {
|
func outputText(j *Job, wg *sync.WaitGroup) {
|
||||||
|
defer wg.Done()
|
||||||
for j.i < j.max {
|
for j.i < j.max {
|
||||||
time.Sleep(1 * time.Millisecond)
|
time.Sleep(1 * time.Millisecond)
|
||||||
fmt.Println(j.text)
|
fmt.Println(j.text)
|
||||||
@ -20,6 +22,7 @@ func outputText(j *Job) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
wg := new(sync.WaitGroup)
|
||||||
hello := new(Job)
|
hello := new(Job)
|
||||||
world := new(Job)
|
world := new(Job)
|
||||||
hello.text = "hello"
|
hello.text = "hello"
|
||||||
@ -28,6 +31,8 @@ func main() {
|
|||||||
world.text = "world"
|
world.text = "world"
|
||||||
world.i = 0
|
world.i = 0
|
||||||
world.max = 5
|
world.max = 5
|
||||||
go outputText(hello)
|
go outputText(hello, wg)
|
||||||
outputText(world)
|
go outputText(world, wg)
|
||||||
|
wg.Add(2)
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user