diff --git a/README.md b/README.md index bacefbd..855359c 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,39 @@ func main() { wg.Wait() fmt.Printf("running goroutines: %d\n", p.Running()) fmt.Printf("finish all tasks, result is %d\n", sum) + if sum != 499500 { + panic("the final result is wrong!!!") + } + + // Use the MultiPool and set the capacity of the 10 goroutine pools to unlimited. + // If you use -1 as the pool size parameter, the size will be unlimited. + // There are two load-balancing algorithms for pools: ants.RoundRobin and ants.LeastTasks. + mp, _ := ants.NewMultiPool(10, -1, ants.RoundRobin) + defer mp.ReleaseTimeout(5 * time.Second) + for i := 0; i < runTimes; i++ { + wg.Add(1) + _ = mp.Submit(syncCalculateSum) + } + wg.Wait() + fmt.Printf("running goroutines: %d\n", mp.Running()) + fmt.Printf("finish all tasks.\n") + + // Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10). + mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) { + myFunc(i) + wg.Done() + }, ants.LeastTasks) + defer mpf.ReleaseTimeout(5 * time.Second) + for i := 0; i < runTimes; i++ { + wg.Add(1) + _ = mpf.Invoke(int32(i)) + } + wg.Wait() + fmt.Printf("running goroutines: %d\n", mpf.Running()) + fmt.Printf("finish all tasks, result is %d\n", sum) + if sum != 499500*2 { + panic("the final result is wrong!!!") + } } ``` diff --git a/README_ZH.md b/README_ZH.md index d3bb007..faa2095 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -122,6 +122,39 @@ func main() { wg.Wait() fmt.Printf("running goroutines: %d\n", p.Running()) fmt.Printf("finish all tasks, result is %d\n", sum) + if sum != 499500 { + panic("the final result is wrong!!!") + } + + // Use the MultiPool and set the capacity of the 10 goroutine pools to unlimited. + // If you use -1 as the pool size parameter, the size will be unlimited. + // There are two load-balancing algorithms for pools: ants.RoundRobin and ants.LeastTasks. + mp, _ := ants.NewMultiPool(10, -1, ants.RoundRobin) + defer mp.ReleaseTimeout(5 * time.Second) + for i := 0; i < runTimes; i++ { + wg.Add(1) + _ = mp.Submit(syncCalculateSum) + } + wg.Wait() + fmt.Printf("running goroutines: %d\n", mp.Running()) + fmt.Printf("finish all tasks.\n") + + // Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10). + mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) { + myFunc(i) + wg.Done() + }, ants.LeastTasks) + defer mpf.ReleaseTimeout(5 * time.Second) + for i := 0; i < runTimes; i++ { + wg.Add(1) + _ = mpf.Invoke(int32(i)) + } + wg.Wait() + fmt.Printf("running goroutines: %d\n", mpf.Running()) + fmt.Printf("finish all tasks, result is %d\n", sum) + if sum != 499500*2 { + panic("the final result is wrong!!!") + } } ``` diff --git a/examples/main.go b/examples/main.go index 3e1d9e5..b967098 100644 --- a/examples/main.go +++ b/examples/main.go @@ -63,7 +63,7 @@ func main() { fmt.Printf("running goroutines: %d\n", ants.Running()) fmt.Printf("finish all tasks.\n") - // Use the pool with a method, + // Use the pool with a function, // set 10 to the capacity of goroutine pool and 1 second for expired duration. p, _ := ants.NewPoolWithFunc(10, func(i interface{}) { myFunc(i) @@ -81,4 +81,34 @@ func main() { if sum != 499500 { panic("the final result is wrong!!!") } + + // Use the MultiPool and set the capacity of the 10 goroutine pools to unlimited. + // If you use -1 as the pool size parameter, the size will be unlimited. + // There are two load-balancing algorithms for pools: ants.RoundRobin and ants.LeastTasks. + mp, _ := ants.NewMultiPool(10, -1, ants.RoundRobin) + defer mp.ReleaseTimeout(5 * time.Second) + for i := 0; i < runTimes; i++ { + wg.Add(1) + _ = mp.Submit(syncCalculateSum) + } + wg.Wait() + fmt.Printf("running goroutines: %d\n", mp.Running()) + fmt.Printf("finish all tasks.\n") + + // Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10). + mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) { + myFunc(i) + wg.Done() + }, ants.LeastTasks) + defer mpf.ReleaseTimeout(5 * time.Second) + for i := 0; i < runTimes; i++ { + wg.Add(1) + _ = mpf.Invoke(int32(i)) + } + wg.Wait() + fmt.Printf("running goroutines: %d\n", mpf.Running()) + fmt.Printf("finish all tasks, result is %d\n", sum) + if sum != 499500*2 { + panic("the final result is wrong!!!") + } }