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 interface{}==nil 的几种坑及原理分析
Apr 24 Golang
go语言中json数据的读取和写出操作
Apr 28 Golang
Go 实现英尺和米的简单单位换算方式
Apr 29 Golang
golang 实现Location跳转方式
May 02 Golang
golang 实现时间戳和时间的转化
May 07 Golang
Golang全局变量加锁的问题解决
May 08 Golang
Go语言基础函数基本用法及示例详解
Nov 17 Golang
深入理解go缓存库freecache的使用
Feb 15 Golang
Go语言的协程上下文的几个方法和用法
Apr 11 Golang
golang连接MySQl使用sqlx库
Apr 14 Golang
Golang 切片(Slice)实现增删改查
Apr 22 Golang
Go调用Rust方法及外部函数接口前置
Jun 14 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
ninety plus是什么?ninety plus咖啡好吗?
2021/03/04 新手入门
用PHP调用Oracle存储过程
2006/10/09 PHP
PHP 压缩文件夹的类代码
2009/11/05 PHP
PHP制作3D扇形统计图以及对图片进行缩放操作实例
2014/10/23 PHP
WordPress特定文章对搜索引擎隐藏或只允许搜索引擎查看
2015/12/31 PHP
php JWT在web端中的使用方法教程
2018/09/06 PHP
在PHP中实现使用Guzzle执行POST和GET请求
2019/10/15 PHP
jquery操作 iframe的方法
2014/12/03 Javascript
JavaScript中数据结构与算法(一):栈
2015/06/19 Javascript
jQuery Validate表单验证入门学习
2015/12/18 Javascript
Bootstrap创建可折叠的组件
2016/02/23 Javascript
Bootstrap路径导航与分页学习使用
2017/02/08 Javascript
老生常谈jacascript DOM节点获取
2017/04/17 Javascript
JS ES6多行字符串与连接字符串的表示方法
2017/04/26 Javascript
jQuery插件开发发送短信倒计时功能代码
2017/05/09 jQuery
详解基于angular-cli配置代理解决跨域请求问题
2017/07/05 Javascript
nodeJs爬虫的技术点总结
2018/05/13 NodeJs
详解Vue前端对axios的封装和使用
2019/04/01 Javascript
Vue实现回到顶部和底部动画效果
2019/07/31 Javascript
vue导航栏部分的动态渲染实例
2019/11/01 Javascript
[00:58]PWL开团时刻DAY5——十人开雾0换5
2020/11/04 DOTA
Python守护线程用法实例
2017/06/23 Python
python实现简单中文词频统计示例
2017/11/08 Python
深入理解Django的中间件middleware
2018/03/14 Python
python监控文件并且发送告警邮件
2018/06/21 Python
django 数据库返回queryset实现封装为字典
2020/05/19 Python
python Scrapy框架原理解析
2021/01/04 Python
HTML5 本地存储实现购物车功能
2017/09/07 HTML / CSS
欧缇丽英国官方网站:Caudalie英国
2016/08/17 全球购物
美国知名女性服饰品牌:New York & Company
2017/03/23 全球购物
Public Desire美国/加拿大:全球性的在线鞋类品牌
2018/12/17 全球购物
货代行业个人求职简历的自我评价
2013/10/22 职场文书
优秀实习自我鉴定
2013/12/04 职场文书
物流管理专业职业生涯规划书
2014/01/06 职场文书
测试工程师职业规划书
2014/02/06 职场文书
Java 超详细讲解数据结构中的堆的应用
2022/04/02 Java/Android