type Ticker struct {
C <-chan Time // The channel on which the ticks are delivered.
r runtimeTimer
}
package main
import (
"time"
"fmt"
)
func main(){
var x int
ticker := time.NewTicker(time.Second * 1) // 运行时长
for {
for x<10{
select {
case <-ticker.C:
x++
fmt.Printf("%d\n", x)
}
}
//ticker.Stop()
}
// 通过通道阻塞,让任务可以执行完指定的次数。
}
type Timer struct {
C <-chan Time
r runtimeTimer
}
package main
import(
"time"
)
func main() {
t1 := time.NewTimer(time.Second * 5)
t2 := time.NewTimer(time.Second * 10)
for {
select{
case <-t1.C:
println("5s timer")
//t1.Reset(time.Second * 5)
break
case <-t2.C:
println("10s timer")
//重新定时
t2.Reset(time.Second )
}
}
}
原文:https://www.cnblogs.com/CYD-self/p/13984964.html