代码拉取完成,页面将自动刷新
package threads
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
var log = func(err interface{}) {
fmt.Println(err)
}
func SetUtilLog(f func(err interface{})) {
log = f
}
//GoTry 在新线程上跑
func GoTry(f func(), catch func(interface{}), finally func()) {
go Try(f, catch, finally)
}
//Try C#中 的try
func Try(f func(), catch func(interface{}), finally func()) {
defer func() {
if finally != nil {
finally()
}
}()
defer func() {
if err := recover(); err != nil {
if catch != nil {
catch(err)
} else {
log(err)
}
}
}()
f()
}
//ThreadRun 新开协程的类有回调用的
type ThreadRun struct {
Chanresult chan func()
}
//NewGoRun 开一个新的协程并运行它
//在新协程上调用f ,resultf回到主线程的方法
func NewGoRun(f func(), resultf func()) *ThreadRun {
result := new(ThreadRun)
result.Chanresult = make(chan func(), 1)
result.Go(f, resultf)
return result
}
//NewGo 开一个新的协程对象
func NewGo() *ThreadRun {
result := new(ThreadRun)
result.Chanresult = make(chan func(), 1)
return result
}
//Go 在新协程上调用f ,resultf回到主线程的方法
func (this *ThreadRun) Go(f func(), resultf func()) {
GoTry(f, nil, func() {
this.Chanresult <- resultf
close(this.Chanresult)
})
}
//ThreadGo 子协程管理计数,可以等子协程都完成
//用它来管理所有开的协程,需要等这些线程都跑完
type ThreadGo struct {
Wg sync.WaitGroup //等待
Ctx context.Context
Cal context.CancelFunc
}
func NewThreadGo() *ThreadGo {
reuslt := new(ThreadGo)
reuslt.Ctx, reuslt.Cal = context.WithCancel(context.Background())
return reuslt
}
func NewThreadGoBySub(ctx context.Context) *ThreadGo {
reuslt := new(ThreadGo)
reuslt.Ctx, reuslt.Cal = context.WithCancel(ctx)
return reuslt
}
func NewThreadGoByGo(thgo *ThreadGo) *ThreadGo {
result := new(ThreadGo)
result.Ctx, result.Cal = context.WithCancel(thgo.Ctx)
return result
}
func (this *ThreadGo) CloseWait() {
this.Cal()
this.Wg.Wait()
}
//Go 在新前线程上跑
func (this *ThreadGo) Go(f func(ctx context.Context)) {
if f == nil {
panic(errors.New("Go func is nil."))
}
this.Wg.Add(1)
GoTry(func() {
f(this.Ctx)
}, nil, func() {
this.Wg.Done()
})
}
//SubGo返回可关掉的子协程
func (this *ThreadGo) SubGo(f func(ctx context.Context)) context.CancelFunc {
if f == nil {
panic(errors.New("Go func is nil."))
}
ctx, cal := context.WithCancel(this.Ctx)
this.Wg.Add(1)
GoTry(func() {
f(ctx)
}, nil, func() {
this.Wg.Done()
})
return cal
}
//GoTry 在新协程上跑
func (this *ThreadGo) GoTry(f func(ctx context.Context), catch func(interface{}), finally func()) {
if f == nil {
panic(errors.New("Go func is nil."))
}
this.Wg.Add(1)
GoTry(
func() {
f(this.Ctx)
},
catch,
func() {
defer this.Wg.Done()
if finally != nil {
finally()
}
})
}
//Try 在当前协程上跑
func (this *ThreadGo) Try(f func(ctx context.Context), catch func(interface{}), finally func()) {
if f == nil {
panic(errors.New("Go func is nil."))
}
this.Wg.Add(1)
Try(
func() {
f(this.Ctx)
},
catch,
func() {
defer this.Wg.Done()
if finally != nil {
finally()
}
})
}
func TimeoutGo(f func(), ticker time.Timer, timeoutfunc func()) {
if f == nil {
panic(errors.New("Go func is nil."))
}
result := make(chan struct{})
GoTry(f, nil, func() {
result <- struct{}{}
close(result)
})
select {
case <-result:
case <-ticker.C:
//写一些一定要打印的信息
if timeoutfunc != nil {
timeoutfunc()
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。