-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_brdm2a.go
52 lines (49 loc) · 900 Bytes
/
17_brdm2a.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
package main
import (
"fmt"
"math/rand"
"time"
)
var steps = [4]string{
"deployBroserv",
"deployBroapp",
"deployBromon",
"deployPAS",
}
func runStep(stepName string) chan bool {
ch := make(chan bool)
go func() {
duration := time.Duration(rand.Intn(1000)) * time.Millisecond
time.Sleep(duration)
fmt.Println("stepName: ", stepName, " elapsed: ", duration, "ms")
ch <- true
}()
return ch
}
func main() {
c := make([]chan bool, 4)
now := time.Now()
for i, v := range steps {
c[i] = runStep(v)
}
done := 0
ch := make(chan bool)
for i := 0; i < 4; i++ {
go func(c chan bool) {
select {
case <-c:
done++
ch <- true
case <-time.After(500 * time.Millisecond):
ch <- true
return
}
}(c[i])
}
for i := 0; i < 4; i++ {
<-ch
}
elapsed := time.Now().Sub(now)
fmt.Println("total: ", elapsed)
fmt.Printf("%d jobs done before timeout\n", done)
}