[云原生] Go 定时器

  • Timer:时间到了,执行只执行1次
  • Ticker:时间到了,多次执行

Timer

  • 基本使用

:::details

func main() {
	/**
	* 1. 创建定时器
	**/
	/**
	NewTimer creates a new Timer that will send the current time on its channel
	after at least duration d
	*/
	log.Println(time.Now())
	timer := time.NewTimer(2 * time.Second)
	fmt.Println(<-timer.C)
	fmt.Printf("%T : %v \r\n", timer, timer)
	log.Println(time.Now())
}

输出

2022/05/17 14:16:23 2022-05-17 14:16:23.9679315 +0800 CST m=+0.001999701
2022-05-17 14:16:25.9945183 +0800 CST m=+2.028580001
*time.Timer : &{0xc000048060 {0 1053841046621200 0 0x9c6520 0xc000048060 0 0 0}} 
2022/05/17 14:16:25 2022-05-17 14:16:25.9953948 +0800 CST m=+2.029456501

:::

  • timer只能用一次

:::details

func main() {
	timer := time.NewTimer(time.Second)

	fmt.Println(<-timer.C)
	fmt.Println("时间到")

	// fmt.Println(<-timer.C)

	fmt.Println("main exit")
}

输出

2022-05-17 14:17:32.8901259 +0800 CST m=+1.014617701
时间到
main exit

把注释打开再运行的结果

2022-05-17 14:18:28.2956576 +0800 CST m=+1.006551901
时间到
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        one.go:14 +0xfa
exit status 2

:::

  • 延时

:::details

func main() {
	// 1. sleep
	log.Println("sleep", time.Now())
	time.Sleep(2 * time.Second)
	log.Println("--------sleep-------")
	// 2. <- time.C
	log.Println("<-time.C", time.Now())
	timer := time.NewTimer(2 * time.Second)
	<-timer.C
	log.Println("-------- <-timer.C -------")
	// 3. <- time.After
	log.Println("<- time.After", time.Now())
	<-time.After(2 * time.Second)
	log.Println("--------after-------")
}

输出

2022/05/17 14:19:21 sleep 2022-05-17 14:19:21.6913174 +0800 CST m=+0.002678801
2022/05/17 14:19:23 --------sleep-------
2022/05/17 14:19:23 <-time.C 2022-05-17 14:19:23.7048594 +0800 CST m=+2.016214301
2022/05/17 14:19:25 -------- <-timer.C -------
2022/05/17 14:19:25 <- time.After 2022-05-17 14:19:25.7141672 +0800 CST m=+4.025515701
2022/05/17 14:19:27 --------after-------

:::

  • Reset

:::details

func main() {
	// 5.重置定时器
	timer5 := time.NewTimer(3 * time.Second)
	timer5.Reset(10 * time.Second)
	fmt.Println(time.Now())
	fmt.Println(<-timer5.C)
}

输出

2022-05-17 14:20:40.9176901 +0800 CST m=+0.002613601
2022-05-17 14:20:50.928108 +0800 CST m=+10.013010401

:::

  • Stop

:::details

func main() {
	// 4.停止定时器
	timer4 := time.NewTimer(2 * time.Second)
	go func() {
		fmt.Println("定时器开始执行")
		<-timer4.C
		fmt.Println("定时器执行完毕")
	}()
	b := timer4.Stop()
	if b {
		fmt.Println("timer4已经关闭")
	}
}

输出

timer4已经关闭

:::

Ticker

  • 跟timer的用法差不多

:::details

package main

import (
	"fmt"
	"time"
)

func main() {
	// 1.获取ticker对象
	ticker := time.NewTicker(2 * time.Second)
	i := 0
	// 子协程
	go func() {
		for {
			//<-ticker.C
			i++
			fmt.Println(<-ticker.C)
			if i == 5 {
				//停止
				ticker.Stop()
			}
		}
	}()
	fmt.Println("main should exit, but sleep")
	time.Sleep(20 * time.Second)
}

输出

main should exit, but sleep
2022-05-17 14:21:43.6481633 +0800 CST m=+2.009463901
2022-05-17 14:21:45.6518969 +0800 CST m=+4.013193301
2022-05-17 14:21:47.6512444 +0800 CST m=+6.012536601
2022-05-17 14:21:49.6423737 +0800 CST m=+8.003661701
2022-05-17 14:21:51.656046 +0800 CST m=+10.017329801

:::

定时器的源码

:::tip

  • 环境
> go version
go version go1.17.7 windows/amd64

:::

timer

:::details

  • 部分其他包的函数
  • runtimeNano : runtimeNano returns the current value of the runtime clock in nanoseconds.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package time

// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
func Sleep(d Duration)

// Interface to timers implemented in package runtime.
// Must be in sync with ../runtime/time.go:/^type timer
type runtimeTimer struct {
	pp       uintptr
	when     int64
	period   int64
	f        func(interface{}, uintptr) // NOTE: must not be closure
	arg      interface{}
	seq      uintptr
	nextwhen int64
	status   uint32
}

// when is a helper function for setting the 'when' field of a runtimeTimer.
// It returns what the time will be, in nanoseconds, Duration d in the future.
// If d is negative, it is ignored. If the returned value would be less than
// zero because of an overflow, MaxInt64 is returned.
func when(d Duration) int64 {
	if d <= 0 {
		return runtimeNano()
	}
	t := runtimeNano() + int64(d)
	if t < 0 {
		// N.B. runtimeNano() and d are always positive, so addition
		// (including overflow) will never result in t == 0.
		t = 1<<63 - 1 // math.MaxInt64
	}
	return t
}

func startTimer(*runtimeTimer)
func stopTimer(*runtimeTimer) bool
func resetTimer(*runtimeTimer, int64) bool
func modTimer(t *runtimeTimer, when, period int64, f func(interface{}, uintptr), arg interface{}, seq uintptr)

// The Timer type represents a single event.
// When the Timer expires, the current time will be sent on C,
// unless the Timer was created by AfterFunc.
// A Timer must be created with NewTimer or AfterFunc.
type Timer struct {
	C <-chan Time
	r runtimeTimer
}

// Stop prevents the Timer from firing.
// It returns true if the call stops the timer, false if the timer has already expired or been stopped.
// Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
//
// To ensure the channel is empty after a call to Stop, check the
// return value and drain the channel.
// For example, assuming the program has not received from t.C already:
//
// 	if !t.Stop() {
// 		<-t.C
// 	}
//
// This cannot be done concurrent to other receives from the Timer's channel or other calls to the Timer's Stop method.
//
// For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer
// has already expired and the function f has been started in its own goroutine;
// Stop does not wait for f to complete before returning.
// If the caller needs to know whether f is completed, it must coordinate
// with f explicitly.
func (t *Timer) Stop() bool {
	if t.r.f == nil {
		panic("time: Stop called on uninitialized Timer")
	}
	return stopTimer(&t.r)
}

// NewTimer creates a new Timer that will send
// the current time on its channel after at least duration d.
func NewTimer(d Duration) *Timer {
	c := make(chan Time, 1)
	t := &Timer{
		C: c,
		r: runtimeTimer{
			when: when(d),
			f:    sendTime,
			arg:  c,
		},
	}
	startTimer(&t.r)
	return t
}

// Reset changes the timer to expire after duration d.
// It returns true if the timer had been active, false if the timer had
// expired or been stopped.
//
// For a Timer created with NewTimer, Reset should be invoked only on
// stopped or expired timers with drained channels.
//
// If a program has already received a value from t.C, the timer is known
// to have expired and the channel drained, so t.Reset can be used directly.
// If a program has not yet received a value from t.C, however,
// the timer must be stopped and—if Stop reports that the timer expired
// before being stopped—the channel explicitly drained:
//
// 	if !t.Stop() {
// 		<-t.C
// 	}
// 	t.Reset(d)
//
// This should not be done concurrent to other receives from the Timer's
// channel.
//
// Note that it is not possible to use Reset's return value correctly, as there
// is a race condition between draining the channel and the new timer expiring.
// Reset should always be invoked on stopped or expired channels, as described above.
// The return value exists to preserve compatibility with existing programs.
//
// For a Timer created with AfterFunc(d, f), Reset either reschedules
// when f will run, in which case Reset returns true, or schedules f
// to run again, in which case it returns false.
// When Reset returns false, Reset neither waits for the prior f to
// complete before returning nor does it guarantee that the subsequent
// goroutine running f does not run concurrently with the prior
// one. If the caller needs to know whether the prior execution of
// f is completed, it must coordinate with f explicitly.
func (t *Timer) Reset(d Duration) bool {
	if t.r.f == nil {
		panic("time: Reset called on uninitialized Timer")
	}
	w := when(d)
	return resetTimer(&t.r, w)
}

func sendTime(c interface{}, seq uintptr) {
	// Non-blocking send of time on c.
	// Used in NewTimer, it cannot block anyway (buffer).
	// Used in NewTicker, dropping sends on the floor is
	// the desired behavior when the reader gets behind,
	// because the sends are periodic.
	select {
	case c.(chan Time) <- Now():
	default:
	}
}

// After waits for the duration to elapse and then sends the current time
// on the returned channel.
// It is equivalent to NewTimer(d).C.
// The underlying Timer is not recovered by the garbage collector
// until the timer fires. If efficiency is a concern, use NewTimer
// instead and call Timer.Stop if the timer is no longer needed.
func After(d Duration) <-chan Time {
	return NewTimer(d).C
}

// AfterFunc waits for the duration to elapse and then calls f
// in its own goroutine. It returns a Timer that can
// be used to cancel the call using its Stop method.
func AfterFunc(d Duration, f func()) *Timer {
	t := &Timer{
		r: runtimeTimer{
			when: when(d),
			f:    goFunc,
			arg:  f,
		},
	}
	startTimer(&t.r)
	return t
}

func goFunc(arg interface{}, seq uintptr) {
	go arg.(func())()
}

:::

ticker

:::details

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package time

import "errors"

// A Ticker holds a channel that delivers ``ticks'' of a clock
// at intervals.
type Ticker struct {
	C <-chan Time // The channel on which the ticks are delivered.
	r runtimeTimer
}

// NewTicker returns a new Ticker containing a channel that will send
// the time on the channel after each tick. The period of the ticks is
// specified by the duration argument. The ticker will adjust the time
// interval or drop ticks to make up for slow receivers.
// The duration d must be greater than zero; if not, NewTicker will
// panic. Stop the ticker to release associated resources.
func NewTicker(d Duration) *Ticker {
	if d <= 0 {
		panic(errors.New("non-positive interval for NewTicker"))
	}
	// Give the channel a 1-element time buffer.
	// If the client falls behind while reading, we drop ticks
	// on the floor until the client catches up.
	c := make(chan Time, 1)
	t := &Ticker{
		C: c,
		r: runtimeTimer{
			when:   when(d),
			period: int64(d),
			f:      sendTime,
			arg:    c,
		},
	}
	startTimer(&t.r)
	return t
}

// Stop turns off a ticker. After Stop, no more ticks will be sent.
// Stop does not close the channel, to prevent a concurrent goroutine
// reading from the channel from seeing an erroneous "tick".
func (t *Ticker) Stop() {
	stopTimer(&t.r)
}

// Reset stops a ticker and resets its period to the specified duration.
// The next tick will arrive after the new period elapses.
func (t *Ticker) Reset(d Duration) {
	if t.r.f == nil {
		panic("time: Reset called on uninitialized Ticker")
	}
	modTimer(&t.r, when(d), int64(d), t.r.f, t.r.arg, t.r.seq)
}

// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only. While Tick is useful for clients that have no need to shut down
// the Ticker, be aware that without a way to shut it down the underlying
// Ticker cannot be recovered by the garbage collector; it "leaks".
// Unlike NewTicker, Tick will return nil if d <= 0.
func Tick(d Duration) <-chan Time {
	if d <= 0 {
		return nil
	}
	return NewTicker(d).C
}

:::

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/273924.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

代码随想录27期|Python|Day29|回溯算法|491.递增子序列|46.全排列|47.全排列 II

491. 非递减子序列 本题不是单纯的去重题目&#xff0c;而是需要保持数字在原数组的顺序。 比如&#xff1a;[4,5,6,7]和[4,6,5,7]相比&#xff0c;后者就不能选择[5,6,7]这个排列&#xff0c;因为违反了设置的顺序。所以去重的方法就只有哈希表。 需要在每一层设置一个哈希表…

注册谷歌企业开发者账号所需的邓白氏码是什么?如何获取?以及相关费用?

随着谷歌政策的收紧&#xff0c;谷歌对个人开发者账号发布应用的要求越来越高&#xff0c;需要20人连续测试14天&#xff0c;才能提审&#xff0c;因此很多开发者选择使用企业账号来进行上架。 而众所周知&#xff0c;注册谷歌企业开发者账号需要邓白氏码。什么是邓白氏码&…

制作系统U盘启动surface教程

最近本人是崩溃的&#xff1a;我surface pro9系统之前被我更新成win11 dev的开发预览版&#xff0c;不好用&#xff0c;有很多bug&#xff0c;升级后才发现已经是预览版成员资格&#xff0c;回退不了&#xff0c;重置初始化依然是dev预览版和取消预览计划是灰色的&#xff0c;退…

Volume Control 2

为游戏添加音乐和音效总是需要一些编码来设置一个系统来控制、显示和保存应用程序的音量设置。 音量控制的设计是为了立即为您设置这些内容,让您有更多时间专注于最重要的事情——制作出色的游戏! 在版本2中,我们对系统进行了重新设计,使其更加模块化、灵活,甚至更易于使用…

MySQL——索引

目录 一.没有索引&#xff0c;可能会有什么问题 二.MySQL与存储 1.先来研究一下磁盘 2.MySQL与磁盘交互基本单位 3.建立共识与总结 三.索引的理解 三.索引操作 1.创建主键索引 2.唯一索引的创建 3.普通索引的创建 4.全文索引的创建 四.查询索引 五.删除索引 一…

MySQL进阶SQL语句

1、select 显示表格种一个或数个字段的所有数据记录 语法&#xff1a;select "字段" from "表名"; 2、distinct 不显示重复的数据记录 语法&#xff1a;select distinct "字段" from "表名"; 3、where 有条件查询 语法&#x…

模式识别与机器学习-SVM(核方法)

SVM&#xff08;核方法&#xff09; 核方法核技巧在SVM中的应用 谨以此博客作为复习期间的记录 核方法 对解线性分类问题&#xff0c;线性分类支持向量机是一种非常有效的方法&#xff0e;但是&#xff0c;有时分类问题是非线性的&#xff0c;这时可以使用非线性支持向量机&a…

线程池原理及使用

线程池继承关系 1.为什么使用线程池&#xff1f; 1.反复创建线程开销大; 2.过多线程会占用太多内存(执行任务易出现“内存溢出”); 3.加快程序响应速度; 4.合理利用CPU和内存; 5.统一管理线程; 2.创建和停止线程池 2.1.线程池参数解释 1.keppAliveTime 如果线程池当中的线程数…

使用Python构建令人瞩目的高频交易算法

大家好&#xff0c;在金融领域&#xff0c;高频交易&#xff08;HFT&#xff09;因其能够以极高的速度执行大量订单的能力而备受关注。高频交易算法旨在识别并利用不同市场间的微小价格差异&#xff0c;因此交易者需要实现低延迟系统来进行套利策略&#xff0c;本文将探索使用P…

我的NPI项目之Android系统升级 - 同平台多产品的OTA

因为公司业务中涉及的面比较广泛&#xff0c;虽然都是提供移动终端PDA&#xff0c;但是使用的场景很多时候是不同的。例如&#xff0c;有提供给大型物流仓储的设备&#xff0c;对这样的设备必需具备扫码功能&#xff0c;键盘&#xff08;戴手套操作&#xff09;&#xff0c;耐用…

大数据求职心得

........................................................................................................................................................... 大数据求职心得 ...................................................................................…

写一个随机点名的程序

获取方式&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1fdCJ_3IYUl7v7x6I1zAWgg 提取码&#xff1a;1234 这里面用到JS当中的数组&#xff0c;random以及window.setInterval&#xff08;&#xff09;回调函数来进行实现的.

性能测试-jemeter:安装 / 基础使用

一、理解jemeter 官网-Apache JMeter-Apache JMeter™ JMeter是一款开源的性能测试工具&#xff0c;主要用于模拟大量用户并发访问目标服务器&#xff0c;以评估服务器的性能和稳定性。 JMeter可以执行以下任务序号用途描述1性能测试通过模拟多个用户在同一时间对服务器进行…

Pytorch从零开始实战14

Pytorch从零开始实战——DenseNet SENet算法实战 本系列来源于365天深度学习训练营 原作者K同学 文章目录 Pytorch从零开始实战——DenseNet SENet算法实战环境准备数据集模型选择开始训练可视化总结 环境准备 本文基于Jupyter notebook&#xff0c;使用Python3.8&#x…

搭建FTP服务器与计算机端口介绍

FTP介绍 FTP&#xff08;File Transfer Protocol&#xff09;是一种用于在计算机网络上进行文件传输的协议。它允许用户通过客户端与服务器进行通信&#xff0c;从服务器下载文件或将文件上传到服务器。 FTP使用客户端-服务器模型。用户使用FTP客户端软件连接到FTP服务器&…

人工智能_机器学习077_Kmeans聚类算法_亚洲国家队自动划分类别_3维可视化实现---人工智能工作笔记0117

然后我们上一节使用聚类算法对,2006年世界杯,2010年世界杯,2007年亚洲杯,足球队进行了自动类别划分,然后 这一节,我们使用代码对,聚类算法的划分结果,进行一下可视化 plt.figure(figsize=(12,9)) 首先指定画布大小 ax=plt.subplot(111,projection=3d) 然后指定111,表示画布的,…

【电商项目实战】基于SpringBoot完成首页搭建

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是Java方文山&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的专栏《电商项目实战》。&#x1f3af;&#x1f3af; &am…

STM32F4系列单片机库函数模板工程创建

目录 一、工程配置 1、新建工程 2、芯片选择 3、工程子文件夹创建 &#xff08;1&#xff09;FWLIB文件夹添加文件 &#xff08;2&#xff09;CORE文件夹添加文件 &#xff08;3&#xff09;USER文件夹添加文件 4、工程设置 &#xff08;1&#xff09;工程中添加文件夹…

Temu和Shein争端再起:海外电商“围城”下,一场厮杀正在酝酿

两家中国电商出海“双子星”&#xff0c;争端再起。 最近&#xff0c;美国法院最新公开临时限制令显示&#xff0c;跨境电商平台Temu&#xff08;特木&#xff09;的男装、休闲装、运动服等50款产品涉侵权时尚电商平台Shein&#xff08;希音&#xff09;&#xff0c;并向Temu旗…

【halcon深度学习】dev_display_dl_data 移植到C# 上篇

效果展示 前言 在研究halcon深度学习的时候,会发现halcon的例程里面用到了大量的二次封装库函数。这些库函数内部也是由基础的算子组成。我们在halcon的开发环境里面用的很爽,但是一旦要在C#中使用,就会报错。 一开始,我想避开这个移植过程,直接使用halcon引擎(HDevEngi…