image.png

channel阻塞

image.pngimage.png
image.pngimage.png

channel阻塞后果

image.png

解决办法

image.png
image.png
image.png

生产者消费者代码实战

_package _main

import (
“fmt”
“time”)

var (
infos = make(_chan _int,10)
)

// 生产者
_func _producer(index int){
infos <- index
fmt.Printf(“Producer : %d, Sent : %d\n”,index,index)
}

// 消费者
_func _consumer(index int){
fmt.Printf(“Consumer : %d, Receive : %d\n”,index,<-infos)
}

func _main(){
// 十个生产者
_for _index := 0; index < 10; index++ {
_go _producer(index)
}
// 十个消费者 _
_for _index := 0; index < 10; index++ {
_go _consumer(index)
}
time.Sleep(20 * time.Second)
}