🏓 Update example code

This commit is contained in:
Andy Pan 2019-02-02 10:30:50 +08:00
parent 834c550921
commit b49328d4e6
1 changed files with 8 additions and 7 deletions

View File

@ -76,27 +76,28 @@ func main() {
runTimes := 1000 runTimes := 1000
// Use the common pool // Use the common pool.
var wg sync.WaitGroup var wg sync.WaitGroup
syncCalculateSum := func() {
demoFunc()
wg.Done()
}
for i := 0; i < runTimes; i++ { for i := 0; i < runTimes; i++ {
wg.Add(1) wg.Add(1)
ants.Submit(func() { ants.Submit(syncCalculateSum)
demoFunc()
wg.Done()
})
} }
wg.Wait() wg.Wait()
fmt.Printf("running goroutines: %d\n", ants.Running()) fmt.Printf("running goroutines: %d\n", ants.Running())
fmt.Printf("finish all tasks.\n") fmt.Printf("finish all tasks.\n")
// Use the pool with a function, // Use the pool with a function,
// set 10 to the size of goroutine pool and 1 second for expired duration // set 10 to the capacity of goroutine pool and 1 second for expired duration.
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) { p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
myFunc(i) myFunc(i)
wg.Done() wg.Done()
}) })
defer p.Release() defer p.Release()
// Submit tasks // Submit tasks one by one.
for i := 0; i < runTimes; i++ { for i := 0; i < runTimes; i++ {
wg.Add(1) wg.Add(1)
p.Serve(int32(i)) p.Serve(int32(i))