diff --git a/main.go b/main.go index 566d56d..344716f 100644 --- a/main.go +++ b/main.go @@ -2,19 +2,31 @@ package main import ( "fmt" - "time" "github.com/xxjwxc/gowp/workerpool" + "github.com/xxjwxc/public/errors" ) -func main() { - wp := workerpool.New(5) //设置最大线程数 +func tets() { + wp := workerpool.New(5) //设置最大线程数 + wp.DoWait(func() error { + for j := 0; j < 10; j++ { + fmt.Println(fmt.Sprintf("%v->\t%v", 000, j)) + } + + return nil + //time.Sleep(1 * time.Second) + //return errors.New("my test err") + }) + for i := 0; i < 10; i++ { //开启10个请求 ii := i wp.Do(func() error { for j := 0; j < 5; j++ { //每次打印0-10的值 fmt.Println(fmt.Sprintf("%v->\t%v", ii, j)) - time.Sleep(1 * time.Second) + if ii == 1 { + return errors.Cause(errors.New("my test err")) //有err 立即返回 + } } return nil }) @@ -24,6 +36,9 @@ func main() { wp.Wait() fmt.Println(wp.IsDone()) - + fmt.Println(wp.IsClosed()) fmt.Println("down") } +func main() { + tets() +} diff --git a/main_test.go b/main_test.go index 63a8ad9..372e41c 100644 --- a/main_test.go +++ b/main_test.go @@ -1,42 +1,9 @@ package main import ( - "fmt" "testing" - - "github.com/xxjwxc/gowp/workerpool" - "github.com/xxjwxc/public/errors" ) func TestMain(t *testing.T) { - wp := workerpool.New(5) //设置最大线程数 - wp.DoWait(func() error { - for j := 0; j < 10; j++ { - fmt.Println(fmt.Sprintf("%v->\t%v", 000, j)) - } - - return nil - //time.Sleep(1 * time.Second) - //return errors.New("my test err") - }) - - for i := 0; i < 10; i++ { //开启10个请求 - ii := i - wp.Do(func() error { - for j := 0; j < 5; j++ { //每次打印0-10的值 - fmt.Println(fmt.Sprintf("%v->\t%v", ii, j)) - if ii == 1 { - return errors.Cause(errors.New("my test err")) //有err 立即返回 - } - } - return nil - }) - - fmt.Println(wp.IsDone()) - } - - wp.Wait() - fmt.Println(wp.IsDone()) - fmt.Println(wp.IsClosed()) - fmt.Println("down") + tets() }