在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
package main import( "fmt" "math/rand" "runtime" "time" ) var total_ticket chan int //define a channel to reserve the count of the ticket func sell(i int){//the params i represents the window number for { count := <- total_ticket if count > 0{ time.Sleep(time.Duration(rand.Intn(5))) total_ticket <- (count -1) fmt.Println("id:",i,"ticket:",count) }else{ break } } } func main(){ runtime.GOMAXPROCS(2) total_ticket = make(chan int,5) total_ticket <- 100 rand.Seed(time.Now().Unix()) //create 5 goroutine to sell tickets for i:=0;i <5;i++{ go sell(i) } //the main function must stop to make the goroutines execute time.Sleep(4e9) }
|
请发表评论