-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinternet-cafe.go
53 lines (43 loc) · 1.02 KB
/
internet-cafe.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
package main
import (
"log"
"math/rand"
"sync"
"time"
)
// http://whipperstacker.com/2015/10/05/3-trivial-concurrency-exercises-for-the-confused-newbie-gopher/
// Solution 3: Internet Cafe
func main() {
rand.Seed(time.Now().UnixNano())
numTourists := 25
maxOnline := 8
var wg sync.WaitGroup
wg.Add(numTourists)
done := make(chan interface{}, maxOnline)
defer close(done)
online := func(done chan interface{}, wg *sync.WaitGroup, i int) {
kv := make(map[int]int, 1)
loop:
select {
case done <- i:
log.Printf("Tourist %d is online.\n", i)
duration := 5 + rand.Intn(10)
time.Sleep(time.Duration(duration) * time.Second)
log.Printf("Tourist %d is done, having spent %d seconds online.\n", i, duration)
<-done
wg.Done()
default:
_, ok := kv[i]
if !ok {
kv[i] = i
log.Printf("Tourist %d waiting for turn.\n", i)
}
goto loop
}
}
for i := 0; i < numTourists; i++ {
go online(done, &wg, i+1)
}
wg.Wait()
log.Println("The place is empty, let's close up and go to the beach!")
}