Go Example--定时器

package main

import (
    "fmt"
    "time"
)

func main() {
    //定时器2s
    timer1 := time.NewTimer(time.Second * 2)
    //读取通道,阻塞2s
    <-timer1.C

    fmt.Println("Timer 1 expired")

    timer2 := time.NewTimer(time.Second)
    go func() {
        <-timer2.C
        fmt.Println("Timer 2 expired")
    }()
    //停止定时器
    stop2 := timer2.Stop()
    if stop2 {
        fmt.Println("Timer 2 stoped")
    }
}

猜你喜欢

转载自www.cnblogs.com/promenader/p/9817290.html