Skip to content

Instantly share code, notes, and snippets.

@jamesBan
Created June 19, 2021 02:59
Show Gist options
  • Save jamesBan/19af9ce608879a3e10967d317d4114c0 to your computer and use it in GitHub Desktop.
Save jamesBan/19af9ce608879a3e10967d317d4114c0 to your computer and use it in GitHub Desktop.
分批执行
package main
import (
"fmt"
"sync"
"time"
)
const num = 10
func main() {
pool := make(chan int, num)
wg := sync.WaitGroup{}
wg.Add(num)
send(pool)
go func() {
for true {
wg.Wait()
send(pool)
wg.Add(num)
}
}()
for true {
<-pool
go func() {
defer wg.Done()
job()
}()
}
}
func job() {
fmt.Println(time.Now())
time.Sleep(time.Second * 2)
}
func send(chan1 chan int) {
fmt.Println("send jobs")
for i:=0; i < num; i++ {
chan1 <- i
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment