-
Notifications
You must be signed in to change notification settings - Fork 20
/
daily-walk.go
69 lines (53 loc) · 1.45 KB
/
daily-walk.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
67
68
69
// http://whipperstacker.com/2015/10/05/3-trivial-concurrency-exercises-for-the-confused-newbie-gopher/
package main
import (
"log"
"math/rand"
"sync"
"time"
)
// http://whipperstacker.com/2015/10/05/3-trivial-concurrency-exercises-for-the-confused-newbie-gopher/
func main() {
rand.Seed(time.Now().UnixNano())
var wg sync.WaitGroup
alice := func() {
log.Println("Alice started getting ready")
duration := 60 + rand.Intn(30)
time.Sleep(time.Second * time.Duration(duration))
log.Printf("Alice spent %v seconds getting ready\n", duration)
wg.Done()
}
bob := func() {
log.Println("Bob started getting ready")
duration := 60 + rand.Intn(30)
time.Sleep(time.Duration(duration) * time.Second)
log.Printf("Bob spent %v seconds getting ready\n", duration)
wg.Done()
}
putOnShoes := func(name string) {
log.Printf("%s started putting on shoes\n", name)
duration := 35 + rand.Intn(10)
time.Sleep(time.Duration(duration) * time.Second)
log.Printf("%s spend %d seconds putting on shoes\n", name, duration)
wg.Done()
}
startAlarm := func() {
log.Println("Arming alarm")
log.Println("Alarm is counting down...")
go putOnShoes("Bob")
go putOnShoes("Alice")
time.Sleep(60 * time.Second)
log.Println("Exiting and locking the door.")
log.Println("Alarm is armed.")
wg.Done()
}
wg.Add(2)
log.Println("Let's go for a walk")
go bob()
go alice()
wg.Wait()
log.Println("Alarm is armed.")
wg.Add(3)
go startAlarm()
wg.Wait()
}