mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-08-03 05:02:16 +02:00
initial import
This commit is contained in:
33
best-practices/goroutine-pooling/main.go
Normal file
33
best-practices/goroutine-pooling/main.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
concurrent = 5
|
||||
pool = make(chan interface{}, concurrent)
|
||||
)
|
||||
|
||||
func work(num int, w *sync.WaitGroup) {
|
||||
pool <- "starting"
|
||||
defer func() {
|
||||
w.Done()
|
||||
<-pool
|
||||
}()
|
||||
fmt.Printf("number %d\n", num)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 1000; i++ {
|
||||
wg.Add(1)
|
||||
go work(i, &wg)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
}
|
Reference in New Issue
Block a user