-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (58 loc) · 1.29 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
type Barber struct {
name string
waitingRoom chan bool
wake chan bool
}
var waitingRoomReats int = 4
var wg sync.WaitGroup
func main() {
newBarber := &Barber{name: "Joe", waitingRoom: make(chan bool, waitingRoomReats), wake: make(chan bool)}
go startBarberShop(newBarber)
//wait a bit for customers
time.Sleep(time.Second * 2)
for i := 0; i < 8; i++ {
wg.Add(1)
go customer(i, newBarber)
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
}
wg.Wait()
fmt.Println("Bye...")
}
func startBarberShop(barber *Barber) {
for {
select {
case <-barber.waitingRoom:
fmt.Println("Start cutting...")
time.Sleep(time.Millisecond * time.Duration(rand.Intn(2000)))
fmt.Println("Stop cutting...")
default:
fmt.Println("let me sleep for some time")
<-barber.wake
}
}
}
func customer(i int, barber *Barber) {
select {
case barber.waitingRoom <- true:
fmt.Println("Client no.", i, "sent message")
select {
case barber.wake <- true:
fmt.Println("Client no.", i, "waked him")
default:
fmt.Println("Client no.", i, "i will wait")
}
wg.Done()
return
default:
fmt.Println("Client no.", i, "no message sent. I'm going home")
time.Sleep(time.Millisecond * 100)
}
wg.Done()
}