golang timer

package main

import (
"fmt"
"time"
)

func main(){
println(time.Now().Unix())
t := time.NewTimer(time.Second*5) //第一次设置
time.Sleep(time.Second*6) //等待超时

    //上次t超时所以,t.Reset返回false,t.C 中有第一次设置时的当前时间
if !t.Reset(time.Second*7) {
tt:=<-t.C
fmt.Println(tt.Unix())
}

select {
case tt:=<-t.C:
fmt.Println(tt.Unix())
fmt.Println("what the screte")
}
fmt.Println("hehe")
}

注意在timer超时,t.C中可以获取设置时的当前时间,如果多次执行 <-t.C就会阻塞.

运行结果:
1510046413
1510046418
1510046426
what the screte
hehe
作者:严彦彪 原创作品转载请注明出处
原文地址:https://www.cnblogs.com/yanbiao/p/7799910.html