1 Star 0 Fork 0

magicianlyx/GoLog

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.94 KB
一键复制 编辑 原始数据 按行查看 历史
magicianlyx 提交于 2022-01-24 17:45 +08:00 . -
package main
import (
"fmt"
"gitee.com/magicianlyx/GoTask/pool"
"math/rand"
"time"
)
func main() {
f2()
}
//type remindSchedule struct {
// firstExecuteDuration time.Duration
// otherExecuteDuration time.Duration
// //lastExecuteTime time.Time
//}
//
//func newRemindSchedule(firstExecuteDuration time.Duration, otherExecuteDuration time.Duration) *remindSchedule {
// r := &remindSchedule{}
// r.firstExecuteDuration = firstExecuteDuration
// r.otherExecuteDuration = otherExecuteDuration
// return r
//}
//
//func (w *remindSchedule) Expression(t *task.TaskInfo) (nt time.Time, isValid bool) {
// //firstExecute
//
// isValid = true
// nt = t.AddTime.Add(w.firstExecuteDuration).Add(w.otherExecuteDuration * time.Duration(t.Count))
// return
//}
//
//func (w *remindSchedule) ToString() string {
// s, _ := jsoniter.MarshalToString(map[string]interface{}{
// "first": fmt.Sprintf("%.6fs", w.firstExecuteDuration.Seconds()),
// "other": fmt.Sprintf("%.6fs", w.otherExecuteDuration.Seconds()),
// })
// return s
//}
//
//func f1() {
// r := GoTask.NewTimedTask(10)
// r.Add("1", func(time int) (map[string]interface{}, error) {
// fmt.Printf("第%v次执行任务\n", time)
// return nil, nil
// }, newRemindSchedule(time.Second*3, time.Second))
//
// time.Sleep(time.Hour)
//}
func f2() {
options := &pool.Options{}
p := pool.NewGoroutinePool(options)
for i := 0; i < 10; i++ {
go func(i int) {
for j := 0; j < 10000000; j++ {
p.Put(func(gid pool.GoroutineUID) {
if gid >= 18 {
fmt.Printf("gid: %v\r\n", gid)
}
rdms := time.Duration(rand.Intn(10)) * time.Millisecond
time.Sleep(rdms)
})
if j%1000 == 0 {
time.Sleep(10 * time.Second)
}
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
}
}(i)
}
go func() {
i := 0
for {
time.Sleep(time.Second)
i++
// fmt.Printf("time.Sleep(time.Second)\r\n")
active := p.GetCurrentActiveCount()
// fmt.Printf("active := pool.GetCurrentActiveCount()\r\n")
count := p.GetGoroutineCount()
// fmt.Printf("count := pool.GetGoroutineCount()\r\n")
peak := p.GetGoroutinePeak()
// fmt.Printf("peak := pool.GetGoroutinePeak()\r\n")
settle := p.GetStatusSettle()
settleMap := map[string]string{}
for i := range settle {
status := i.ToString()
duration := fmt.Sprintf("%v", settle[i])
settleMap[status] = duration
}
fmt.Printf("active: %v count: %v peak: %v channel: %v active_duration: %v sleep_duration: %v \r\n",
active,
count,
peak,
p.GetWorkCount(),
settleMap["active"],
settleMap["sleep"],
)
// fmt.Printf("%s\r\n",
// utils.ToJson(settleMap),
// )
_ = active
_ = count
_ = peak
_ = settle
}
}()
go func() {
time.Sleep(time.Second * 20)
p.Stop()
p.Stop()
p.Stop()
fmt.Printf("关闭组件\r\n")
}()
// go func() {
// for {
// time.Sleep(time.Second)
// fmt.Printf("channel: %v\r\n", len(pool.c))
// }
// }()
time.Sleep(time.Hour)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magicianlyx/GoLog.git
git@gitee.com:magicianlyx/GoLog.git
magicianlyx
GoLog
GoLog
20c45f9b998d

搜索帮助