go语言定时器Timer及Ticker的功能使用示例详解
作者:Jeff的技术栈 时间:2024-02-09 09:17:45
定时器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
}
}
}
来源:https://www.cnblogs.com/guyouyin123/p/15772355.html
标签:go,Timer,Ticker,定时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
golang语言map全方位介绍
2024-05-22 10:20:33
![](https://img.aspxhome.com/file/2023/3/123863_0s.jpg)
python用线性回归预测股票价格的实现代码
2023-01-24 02:14:31
![](https://img.aspxhome.com/file/2023/9/94129_0s.png)
js实现用div层模拟的小窗口
2007-12-02 15:01:00
![](https://img.aspxhome.com/file/UploadPic/200712/2/2007122151314277s.gif)
python使用openpyxl操作excel的方法步骤
2022-09-30 20:59:24
![](https://img.aspxhome.com/file/2023/6/101446_0s.jpg)
Oracle重建控制文件的实例教程
2024-01-25 11:02:16
Python文件读写w+和r+区别解析
2022-01-12 04:23:04
Mootools 1.2教程(8)——输入过滤第一部分(数字)
2008-11-27 13:01:00
深入理解javascript中defer的作用
2024-04-16 10:32:55
详解SQL游标的用法
2024-01-18 02:41:30
![](https://img.aspxhome.com/file/2023/2/105462_0s.png)
python logging模块的使用详解
2023-06-08 08:43:34
![](https://img.aspxhome.com/file/2023/5/68495_0s.png)
sklearn+python:线性回归案例
2023-10-19 20:07:01
python for循环赋值问题
2023-01-26 05:56:32
超强多功能php绿色集成环境详解
2023-07-15 01:07:22
![](https://img.aspxhome.com/file/2023/2/55472_0s.jpg)
Anaconda 离线安装 python 包的操作方法
2021-08-11 22:38:27
![](https://img.aspxhome.com/file/2023/1/135411_0s.jpg)
Python提取频域特征知识点浅析
2021-10-31 08:01:31
![](https://img.aspxhome.com/file/2023/3/75323_0s.jpg)
Pycharm安装第三方库时Non-zero exit code错误解决办法
2023-03-15 12:15:01
![](https://img.aspxhome.com/file/2023/2/121732_0s.png)
python3.6.3转化为win-exe文件发布的方法
2021-02-16 05:57:40
使用jupyter notebook运行python和R的步骤
2023-03-30 18:22:50
![](https://img.aspxhome.com/file/2023/0/80410_0s.png)
Python的类实例属性访问规则探讨
2023-04-07 08:09:17
Go语言题解LeetCode705设计哈希集合
2024-03-19 22:30:38