-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-routines.go
66 lines (53 loc) · 824 Bytes
/
go-routines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"sync"
"time"
)
func worker(id int, wg *sync.WaitGroup) {
/**
Logging for the new worker
*/
fmt.Printf("Worker %d starting\n", id)
/**
Faking the heavy process
*/
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
/**
messaging that this worker is completed
*/
wg.Done()
}
func main() {
var wg sync.WaitGroup
for i := 1; i <= 2; i++ {
/**
Adding a new worker in the waiting list
*/
wg.Add(1)
/**
go routine to start a new worker
*/
go worker(i, &wg)
}
wg.Wait()
}
/**
several output of this experiment
output 1:
Worker 2 starting
Worker 1 starting
Worker 2 done
Worker 1 done
output 2:
Worker 1 starting
Worker 2 starting
Worker 1 done
Worker 2 done
output 3:
Worker 1 starting
Worker 2 starting
Worker 1 done
Worker 2 done
*/