1. Generator
9. Queue
package main
import (
"fmt"
"sync"
"time"
)
const limit = 4
const work = 100
func process(wg *sync.WaitGroup, work int, queue chan struct{}) {
queue <- struct{}{}
go func() {
defer wg.Done()
time.Sleep(time.Second * 1)
fmt.Println("processed: ", work)
<-queue
}()
}
func main() {
var wg sync.WaitGroup
fmt.Println("Queue limit: ", limit)
queue := make(chan struct{}, limit)
wg.Add(work)
for w := 0; w < work; w++ {
process(&wg, w, queue)
}
wg.Wait()
close(queue)
fmt.Println("work complete")
}
标签:queue,work,const,编程,sync,golang,并发
From: https://www.cnblogs.com/liujitao79/p/17271552.html