3 Star 1 Fork 0

NightTC/Gobige

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
common
dblink
dbmanager
entity
etcd
ginhttp
global
kafka
linmath
logger
msgdef
msghandler
pool
redislib
safecontainer
serverMgr
service
sess
space
threads
ThreadRun.go
timerwheel
.gitignore
LICENSE
go.mod
gobige_test.go
克隆/下载
ThreadRun.go 3.69 KB
一键复制 编辑 原始数据 按行查看 历史
buguang 提交于 2年前 . threads
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()
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/night-tc/gobige.git
git@gitee.com:night-tc/gobige.git
night-tc
gobige
Gobige
e6ef893c0671

搜索帮助