限制单位时间内执行次数,并保持先后顺序。
package main
import "fmt"
import "time"
var c chan chan int
func worker(i int) {
t := make(chan int)
c <- t
fmt.Println(i, "启动任务")
loop:
for {
select {
case <-t:
fmt.Println(i)
break loop
default:
fmt.Println(i, "等待可用")
time.Sleep(100 * time.Millisecond)
}
}
fmt.Println(i, "开始任务")
}
func master() {
for {
for i := 0; i < 10; i++ {
t := <-c
t <- 1
time.Sleep(500 * time.Millisecond)
}
}
}
func main() {
c = make(chan chan int, 20)
for i := 0; i < 10; i++ {
go worker(i)
time.Sleep(500 * time.Millisecond)
}
go master()
for {
time.Sleep(1000 * time.Millisecond)
}
}
标签:none