-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_case
More file actions
70 lines (60 loc) · 1.6 KB
/
Copy pathselect_case
File metadata and controls
70 lines (60 loc) · 1.6 KB
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
70
package main
import (
"fmt"
"time"
)
func doWorker(c chan int, quit chan bool) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("it is time to quit")
return
}
}
}
func main() {
fmt.Println("main routine start")
//demo 1 :主要练习 select case 的用法
ch := make(chan int)
timeout := make(chan bool, 1)
go func() {
//第一种方式:注释 time。Sleep(1e9),两个case 都满足,go 随机选择一个执行
// //time.Sleep(1e9) /
// timeout <- true
// ch <- 3
//第二种方式:ch <- 3,case 输出 ch
// ch <- 3
// time.Sleep(1e9)
// timeout <- true
//第三种方式:两个chan 没有send ,case 输出 default,这种情况下若没有default 分支,造成deadlock
//ch <- 3
time.Sleep(1e9)
//timeout <- true
}()
select {
case val := <-ch:
fmt.Println("ch...val:", val)
case <-timeout:
fmt.Println("timeout")
default:
fmt.Println("default")
}
//demo 2:主要练习 开启的goroutine 完成操作后,发送信号通知main gorotine 结束。
// c := make(chan int)
// quit := make(chan bool)
// go func() {
// for i := 0; i < 10; i++ {
// fmt.Println(<-c)
// }
// //执行操作后,发生quit信号,通知main routine 结束
// quit <- true
// }()
// doWorker(c, quit)
fmt.Println("main routine end")
}
//如果有同时多个case接收数据,那么Go会伪随机的选择一个case处理(pseudo-random)。
//如果没有case需要处理,则会选择default去处理。
//如果没有default case,且有发送的chan 则select语句会阻塞,直到某个case需要处理。