新增时间轮定时器

This commit is contained in:
boyce
2020-10-24 16:55:55 +08:00
parent 18281e2b09
commit 2b05358f04
5 changed files with 171 additions and 194 deletions

View File

@@ -1,74 +1,31 @@
package timewheel
import (
"fmt"
"testing"
"time"
"fmt"
)
var timerCount int
var mapId map[int] interface{}
func Test_Example(t *testing.T) {
now := time.Now()
timer := NewTimer(time.Millisecond*20)
timer:=NewTimer(time.Second*2)
select {
case <-timer.C:
fmt.Print("xxx:")
}
fmt.Println(time.Now().Sub(now).Milliseconds())
/*
rand.Seed(time.Now().UnixNano())
timeWheel := NewTimeWheel()
mapId = map[int] interface{}{}
time.Sleep(time.Duration(rand.Intn(100))*time.Millisecond)
timeWheel.Tick()
time.AfterFunc()
for i:=100000000;i<200000000;i++{
r := rand.Intn(100)
timeWheel.AddTimer(i+r,func(){
fmt.Print("+\n")
})
time.NewTicker()
time.AfterFunc()
timerCount+=1
case <- timer.C:
fmt.Println("It is time out!")
}
fmt.Println("add finish..")
go func(){
for{
select {
case t:=<-timeWheel.chanTimer:
timerCount--
if timerCount == 0 {
fmt.Printf("finish...\n")
}
if t.tmp-t.expireTicks >1 {
fmt.Printf("err:%d:%d\n",t.expireTicks,t.tmp-t.expireTicks)
}else{
if t.expireTicks%100000 == 0 {
fmt.Printf("%d:%d:%d\n",t.expireTicks,t.tmp-t.expireTicks,t.tmpMilSeconds)
}
//t.timerCB()
}
}
}
}()
for{
timeWheel.TickOneFrame()
//time.Sleep(1*time.Microsecond)
//fmt.Println(".")
timer2 := NewTimerEx(time.Second*2,nil,1)
select {
case t:=<- timer2.C:
fmt.Println("It is time out!",t.AdditionData.(int))
}
timer3 := NewTimerEx(time.Second*2,nil,1)
timer3.Stop()
time.Sleep(3*time.Second)
select {
case t:=<- timer2.C:
fmt.Println("It is time out!",t.AdditionData.(int))
default:
fmt.Printf("time is stop")
}
*/
}