-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchan1.go
55 lines (51 loc) · 1.29 KB
/
chan1.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
package main
import (
"fmt"
"sync"
"time"
)
var wC1 sync.WaitGroup
// 无缓冲channel无论发送操作还是接受操作一开始就是阻塞的,只有配对的操作出现才会开始执行
// 有缓冲这会在 已满/已空情况下的发送/接受操作会阻塞
func main() {
wC1.Add(6)
chNoBuffer := make(chan int)
chBuffer1 := make(chan int, 3)
go func() {
defer wC1.Done()
fmt.Println("start consumer nobuffer chan")
fmt.Printf("nobuffer chan is %d \n", <-chNoBuffer)
}()
go func() {
defer wC1.Done()
fmt.Println("start product nobuffer chan")
time.Sleep(3 * time.Second)
chNoBuffer <- 123
fmt.Println("product nobuffer chan done")
}()
go func() {
defer wC1.Done()
fmt.Println("start product nobuffer chan 2")
time.Sleep(6 * time.Second)
chNoBuffer <- 456
fmt.Println("product nobuffer chan done 2")
}()
go func() {
defer wC1.Done()
fmt.Println("start consumer nobuffer chan 2")
fmt.Printf("nobuffer chan is %d 2\n", <-chNoBuffer)
}()
go func() {
defer wC1.Done()
fmt.Println("start consumer chBuffer1 chan")
fmt.Printf("chBuffer1 chan is %d \n", <-chBuffer1)
}()
go func() {
defer wC1.Done()
fmt.Println("start product chBuffer1 chan")
chBuffer1 <- 789
fmt.Println("product chBuffer1 chan done")
}()
wC1.Wait()
fmt.Println("done")
}