golang定时器


Posted in Golang onApril 14, 2022

定时器1-"*/5 * * * * *"

package main
import (
	"fmt"

	"github.com/robfig/cron"
)
//主函数
func main() {
	cron2 := cron.New() //创建一个cron实例
	//执行定时任务(每5秒执行一次)
	err:= cron2.AddFunc("*/5 * * * * *", print5)
	if err!=nil{
		fmt.Println(err)
	}
	//启动/关闭
	cron2.Start()
	defer cron2.Stop()
	select {
	//查询语句,保持程序运行,在这里等同于for{}
	}
}
//执行函数
func print5()  {
	fmt.Println("每5s执行一次cron")
}

设置说明

┌─────────────second 范围 (0 - 60)
 │ ┌───────────── min (0 - 59)
 │ │ ┌────────────── hour (0 - 23)
 │ │ │ ┌─────────────── day of month (1 - 31)
 │ │ │ │ ┌──────────────── month (1 - 12)
 │ │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
 │ │ │ │ │ │                  Saturday)
 │ │ │ │ │ │
 │ │ │ │ │ │
 * * * * * *

星号(*) :表示 cron 表达式能匹配该字段的所有值。如在第5个字段使用星号(month),表示每个月

斜线(/):表示增长间隔,如第2个字段(minutes) 值是 3-59/15,表示每小时的第3分钟开始执行一次,之后 每隔 15 分钟执行一次(即 3(3+0*15)、18(3+1*15)、33(3+2*15)、48(3+3*15) 这些时间点执行),这里也可以表示为:3/15

逗号(,):用于枚举值,如第6个字段值是 MON,WED,FRI,表示 星期一、三、五 执行

连字号(-):表示一个范围,如第3个字段的值为 9-17 表示 9am 到 5pm 直接每个小时(包括9和17)

问号(?):只用于 日(Day of month) 和 星期(Day of week),表示不指定值,可以用于代替 *

定时器2-Timer-Ticker

Timer  //时间到了产生一次事件
Ticker  //时间到了循环产生事件

Timer-只执行一次

package main
import (
	"fmt"
	"time"
)
func main() {
	//创建一个定时器,设置时间为2s,2s后,往time通道写内容(当前时间)
	timer := time.NewTimer(2 * time.Second)
	fmt.Println("当前时间:", time.Now())
	//2s后,往timer.C写数据,有数据后,就可以读取
	t := <-timer.C //channel没有数据前后阻塞
	fmt.Println("t = ", t)
}

Ticker-循环执行

package main
import (
	"fmt"
	"time"
)
//验证time.NewTimer(),时间到了,只会响应一次
func main() {
	timer := time.NewTicker(1 * time.Second)
	for {
		<-timer.C
		fmt.Println("时间到")
	}
}

结果:

时间到
时间到
时间到
时间到

Timer延时功能

time.NewTimer(2 * time.Second)  //相当于time.Sleep(2 * time.Second)
func main() {
	//延时2s后打印一句话
	timer := time.NewTimer(2 * time.Second)
	<-timer.C
	fmt.Println("时间到")
}
func main() {
	<-time.After(2 * time.Second) //定时2s,阻塞2s, 2s后产生一个事件,往channel写内容
	fmt.Println("时间到")
}

停止和重置定时器

ok := timer.Reset(1 * time.Second) //重新设置为1s
timer.Stop() //停止定时器

停止:

func main() {
	timer := time.NewTimer(3 * time.Second)
	go func() {
		<-timer.C
		fmt.Println("子协程可以打印了,因为定时器的时间到")
	}()
	timer.Stop() //停止定时器
	for {
	}
}

重置:

func main() {
	timer := time.NewTimer(3 * time.Second)
	ok := timer.Reset(1 * time.Second) //重新设置为1s
	fmt.Println("ok = ", ok)
	<-timer.C
	fmt.Println("时间到")
}

定时器Ticker使用

package main
import (
	"fmt"
	"time"
)
func main() {
	ticker := time.NewTicker(1 * time.Second)
	i := 0
	for {
		<-ticker.C
		i++
		fmt.Println("i = ", i)
		if i == 5 {
			ticker.Stop()
			break
		}
	}
}

以上就是go语言定时器的功能使用示例详解的详细内容!

Golang 相关文章推荐
为什么不建议在go项目中使用init()
Apr 12 Golang
Go语言-为什么返回值为接口类型,却返回结构体
Apr 24 Golang
Golang 正则匹配效率详解
Apr 25 Golang
golang 实现对Map进行键值自定义排序
Apr 28 Golang
Go 实现英尺和米的简单单位换算方式
Apr 29 Golang
Go语言设计模式之结构型模式
Jun 22 Golang
Go 语言中 20 个占位符的整理
Oct 16 Golang
Go语言的协程上下文的几个方法和用法
Apr 11 Golang
Golang数据类型和相互转换
Apr 12 Golang
Golang ort 中的sortInts 方法
Apr 24 Golang
Go 内联优化让程序员爱不释手
Jun 21 Golang
golang用type-switch判断interface的实际存储类型
Apr 14 #Golang
golang语言指针操作
Apr 14 #Golang
golang使用map实现去除重复数组
Apr 14 #Golang
golang生成并解析JSON
Apr 14 #Golang
Go语言 详解net的tcp服务
Apr 14 #Golang
golang连接MySQl使用sqlx库
Apr 14 #Golang
Go语言安装并操作redis的go-redis库
Apr 14 #Golang
You might like
php简单静态页生成过程
2008/03/27 PHP
PHP操作mysql函数详解,mysql和php交互函数
2011/05/19 PHP
PHP实现手机归属地查询API接口实现代码
2012/08/27 PHP
php中将一段数据存到一个txt文件中并显示其内容
2014/08/15 PHP
php中类和对象:静态属性、静态方法
2017/04/09 PHP
javascript在一段文字中的光标处插入其他文字
2007/08/26 Javascript
JS正则中的RegExp对象对象
2012/11/07 Javascript
动态加载jquery库的方法
2014/02/12 Javascript
js关于命名空间的函数实例
2015/02/05 Javascript
jQuery实现的fixedMenu下拉菜单效果代码
2015/08/24 Javascript
element 结合vue 在表单验证时有值却提示错误的解决办法
2018/01/22 Javascript
vue2.0获取鼠标位置的方法
2018/09/13 Javascript
微信jssdk逻辑在vue中的运用详解
2018/11/14 Javascript
RxJS在TypeScript中的简单使用详解
2020/04/13 Javascript
vue实现多个echarts根据屏幕大小变化而变化实例
2020/07/19 Javascript
Vue循环中多个input绑定指定v-model实例
2020/08/31 Javascript
[48:31]完美世界DOTA2联赛PWL S3 DLG vs Phoenix 第二场 12.17
2020/12/19 DOTA
Python的面向对象思想分析
2015/01/14 Python
Python当中的array数组对象实例详解
2019/06/12 Python
django认证系统实现自定义权限管理的方法
2019/08/28 Python
python 线性回归分析模型检验标准--拟合优度详解
2020/02/24 Python
python代码中怎么换行
2020/06/17 Python
用python实现名片管理系统
2020/06/18 Python
Python实现弹球小游戏
2020/08/01 Python
Python通过yagmail实现发送邮件代码解析
2020/10/27 Python
详解用 python-docx 创建浮动图片
2021/01/24 Python
幼儿园教师节活动方案
2014/02/02 职场文书
职工擅自离岗检讨书
2014/09/23 职场文书
党的群众路线教育实践活动整改落实情况报告
2014/10/28 职场文书
精神文明建设先进个人事迹材料
2014/12/24 职场文书
廉政承诺书2015
2015/04/28 职场文书
建国大业观后感600字
2015/06/01 职场文书
安全教育主题班会总结
2015/08/14 职场文书
人为什么会“幸灾乐祸”?
2019/08/06 职场文书
2019垃圾分类宣传口号汇总
2019/08/16 职场文书
解决Pytorch dataloader时报错每个tensor维度不一样的问题
2021/05/28 Python