商君

导航

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")
	}
}

posted on 2018-10-19 16:40  漫步者01  阅读(105)  评论(0编辑  收藏  举报