-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/panjf2000/ants/v2" | ||
) | ||
|
||
var sum int32 | ||
|
||
func myFunc(i interface{}) { | ||
n := i.(int32) | ||
atomic.AddInt32(&sum, n) | ||
fmt.Printf("run with %d\n", n) | ||
} | ||
|
||
func demoFunc() { | ||
time.Sleep(10 * time.Millisecond) | ||
fmt.Println("Hello World!") | ||
} | ||
|
||
func main() { | ||
defer ants.Release() | ||
|
||
runTimes := 1000 | ||
|
||
// Use the common pool. | ||
var wg sync.WaitGroup | ||
syncCalculateSum := func() { | ||
demoFunc() | ||
wg.Done() | ||
} | ||
for i := 0; i < runTimes; i++ { | ||
wg.Add(1) | ||
_ = ants.Submit(syncCalculateSum) | ||
} | ||
wg.Wait() | ||
fmt.Printf("running goroutines: %d\n", ants.Running()) | ||
fmt.Printf("finish all tasks.\n") | ||
|
||
// 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) | ||
wg.Done() | ||
}) | ||
defer p.Release() | ||
// Submit tasks one by one. | ||
for i := 0; i < runTimes; i++ { | ||
wg.Add(1) | ||
_ = p.Invoke(int32(i)) | ||
} | ||
|
||
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!!!") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/panjf2000/ants/v2" | ||
"log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func wrapper(i int, wg *sync.WaitGroup) func() { | ||
return func() { | ||
fmt.Printf("hello from task:%d\n", i) | ||
if i%2 == 0 { | ||
panic(fmt.Sprintf("panic from task:%d", i)) | ||
} | ||
wg.Done() | ||
} | ||
} | ||
|
||
func ServicePanicHandler(info interface{}) { | ||
c := fmt.Sprintf("%#v", info) | ||
log.Println("ServicePanicHandler handler: ", c) | ||
|
||
} | ||
|
||
func main() { | ||
p, _ := ants.NewPool(2, ants.WithPanicHandler(ServicePanicHandler)) | ||
defer p.Release() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(3) | ||
for i := 1; i <= 2; i++ { | ||
p.Submit(wrapper(i, &wg)) | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
p.Submit(wrapper(3, &wg)) | ||
p.Submit(wrapper(5, &wg)) | ||
p.Submit(wrapper(6, &wg)) | ||
wg.Wait() | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package sync_ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sync_ | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
type Aa struct { | ||
a int | ||
b float64 | ||
c []int | ||
} | ||
|
||
func TestSemaphore(t *testing.T) { | ||
c := Aa{} | ||
fmt.Println(unsafe.Sizeof(c)) | ||
c.a = 55 | ||
c.b = 99 | ||
fmt.Println(unsafe.Sizeof(c)) | ||
|
||
} |