Created
June 19, 2021 02:59
-
-
Save jamesBan/19af9ce608879a3e10967d317d4114c0 to your computer and use it in GitHub Desktop.
分批执行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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