package main import ( "fmt" "strconv" "time" ) /* 改进生产者消费者模型 ·生产者每秒生产一件商品,并通知物流公司取货 ·物流公司将商品运输到商店 ·消费者阻塞等待从商店消费 ·消费10轮就主协程结束 ·尝试在整分钟时通知生产者罢工,生产者罢工时主协程结束 ·计时协程,不断地查看有没有到整分钟 ·如果到整分钟,将【全局罢工变量】置为true ·生产者每完成一轮生产就查看【全局罢工变量】,如果为true就向【chGoDie】写入值 */ type Product struct { Name string } var chGoDie = make(chan interface{}) func main() { chStorage := make(chan Product, 5) chShop:= make(chan Product, 5) go Producer(chStorage) go Logistics(chStorage, chShop) go Consumer(chShop) go Watcher() //阻塞等待狗带命令 <-chGoDie fmt.Println("main:啊!我死了") } /*计时协程*/ var Bagong = false func Watcher() { for true { second := time.Now().Second() fmt.Println("当前秒数", second) if second ==0{ fmt.Println("罢工开始!!!!!") Bagong = true return } time.Sleep(time.Second) } } /*物流公司*/ func Logistics(chStorage chan Product, chShop chan Product) { for true { product:=<-chStorage chShop <- product fmt.Println("转运了",product) } } /*生产者*/ func Producer(chStorage chan Product) { for { product := Product{"商品" + strconv.Itoa(time.Now().Second())} chStorage<-product fmt.Println("生产了",product) if Bagong { fmt.Println("收到!Go Die主协程") chGoDie <- "Go Die!" } time.Sleep(time.Second) } } /*消费者*/ func Consumer(chShop chan Product) { for { product :=<- chShop fmt.Println("消费了",product) } }