timer
// We often want to execute Go code at some point in the
// future, or repeatedly at some interval. Go's built-in
// _timer_ and _ticker_ features make both of these tasks
// easy. We'll look first at timers and then
// at [tickers](tickers).
package main
import "time"
import "fmt"
func main() {
// Timers represent a single event in the future. You
// tell the timer how long you want to wait, and it
// provides a channel that will be notified at that
// time. This timer will wait 2 seconds.
timer1 := time.NewTimer(time.Second * 2)
// The `<-timer1.C` blocks on the timer's channel `C`
// until it sends a value indicating that the timer
// expired.
<-timer1.C
fmt.Println("Timer 1 expired")
// If you just wanted to wait, you could have used
// `time.Sleep`. One reason a timer may be useful is
// that you can cancel the timer before it expires.
// Here's an example of that.
timer2 := time.NewTimer(time.Second)
go func() {
<-timer2.C
fmt.Println("Timer 2 expired")
}()
//如果想要看到timer2的执行结果,可以让main函数sleep一会等待goroutine执行结束
//time.Sleep(time.Second * 2)
stop2 := timer2.Stop()
if stop2 {
fmt.Println("Timer 2 stopped")
}
}
//输出
Timer 1 expired
Timer 2 stopped
ticker
// [Timers](timers) are for when you want to do
// something once in the future - _tickers_ are for when
// you want to do something repeatedly at regular
// intervals. Here's an example of a ticker that ticks
// periodically until we stop it.
package main
import "time"
import "fmt"
func main() {
// Tickers use a similar mechanism to timers: a
// channel that is sent values. Here we'll use the
// `range` builtin on the channel to iterate over
// the values as they arrive every 500ms.
ticker := time.NewTicker(time.Millisecond * 500)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
}
}()
// Tickers can be stopped like timers. Once a ticker
// is stopped it won't receive any more values on its
// channel. We'll stop ours after 1600ms.
time.Sleep(time.Millisecond * 1600)
ticker.Stop()
fmt.Println("Ticker stopped")
}
//输出
Tick at 2009-11-10 23:00:00.5 +0000 UTC m=+0.500000000
Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000000
Tick at 2009-11-10 23:00:01.5 +0000 UTC m=+1.500000000
Ticker stopped
参考文章:
timer ticker
登陆发表评论