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
package main
import "time"
import (
"fmt"
"math/rand"
"strconv"
)
func main() {
c1 := make(chan string, 10)
flag := make (chan bool)
count := 0
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for j := 1; j < 6; j++ {
go func() {
for i := 1; i < 10; i++ {
fmt.Println(rand.Intn(10))
time.Sleep(time.Duration(rand.Intn(10)) * time.Second)
c1 <- strconv.Itoa(i) + "--result 1"
}
}()
}
for j := 1; j < 6; j++ {
go func() {
for {
select {
case res := <-c1:
fmt.Println(res)
case <-ticker.C:
count = count + 1
fmt.Println("timeout 1")
}
if count > 10 {
flag <- true
break
}
}
}()
}
<- flag
}