Created
February 21, 2021 05:52
-
-
Save x893675/580e5b73ec1e1d2fc1847834ec817307 to your computer and use it in GitHub Desktop.
timer and ticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
/** | |
*ticker只要定义完成,从此刻开始计时,不需要任何其他的操作,每隔固定时间都会触发。 | |
*timer定时器,是到固定时间后会执行一次 | |
*如果timer定时器要每隔间隔的时间执行,实现ticker的效果,使用 func (t *Timer) Reset(d Duration) bool | |
*/ | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(2) | |
//NewTimer 创建一个 Timer,它会在最少过去时间段 d 后到期,向其自身的 C 字段发送当时的时间 | |
timer1 := time.NewTimer(2 * time.Second) | |
//NewTicker 返回一个新的 Ticker,该 Ticker 包含一个通道字段,并会每隔时间段 d 就向该通道发送当时的时间。它会调 //整时间间隔或者丢弃 tick 信息以适应反应慢的接收者。如果d <= 0会触发panic。关闭该 Ticker 可以释放相关资源。 | |
ticker1 := time.NewTicker(2 * time.Second) | |
go func(t *time.Ticker) { | |
defer wg.Done() | |
for { | |
<-t.C | |
fmt.Println("get ticker1", time.Now().Format(time.RFC3339)) | |
} | |
}(ticker1) | |
go func(t *time.Timer) { | |
defer wg.Done() | |
for { | |
<-t.C | |
fmt.Println("get timer", time.Now().Format(time.RFC3339)) | |
//Reset 使 t 重新开始计时,(本方法返回后再)等待时间段 d 过去后到期。如果调用时 t 还在等待中会返回真;如果 t已经到期或者被停止了会返回假。 | |
t.Reset(2 * time.Second) | |
} | |
}(timer1) | |
timer1.Stop() | |
ticker1.Stop() | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment