首页 > 代码库 > golang for thread channel routine consumer and producer
golang for thread channel routine consumer and producer
package main import "time" func testThread(){ i:=3 go func(a int){ println(a) println("this is go func!") }(i) time.Sleep(1*time.Second) println("hello main thread!") } func testChannel(){ //read write channel ch:=make(chan int,1) ch<-3 go func(){ v:=<-ch println(v) }() time.Sleep(1*time.Second) println("finish chan!") } func producer(p chan<-int){ for i:=0;i<10;i++{ p<-i println("into chan:",i) } println("finish the producer!") } func consumers(c <-chan int){ for i:=0;i<10;i++{ v:=<-c println("get from channel:",v) } println("finish the consumers!!") } //test producer and consumer func testProducerConsumer(){ ch:=make(chan int,1) go consumers(ch) go producer(ch) time.Sleep(2*time.Second) println("finish all the consumer and producer!!") } func main() { //testThread() //testChannel() testProducerConsumer() }
熟悉语法:chan,go多线程
golang for thread channel routine consumer and producer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。