SoFunction
Updated on 2025-03-01

Go's fixed-duration timer and periodic timer

In the past, if we wanted to implement delayed execution in the scheduling, we could use pipeline blocking until someone wrote something into the pipeline to become smoother. We could also use sleep to sleep, but during the sleeping process, the coroutine could not do anything and occupied resources. So we need to use the timer we talk about next, and we will not take up resources when sleeping like sleep.

Let’s take a look at the following code:

package main
​
import (
    "fmt"
    "time"
)
​
func main() {
    timer := (3 * )
    ("The timer is created!")
    (())
    //The time can be read after blocking for 3 seconds    x := <- 
    //This C is a one-way read-only pipeline    (x)
}

The result of the operation is as follows:

The timer is created!
2021-08-24 14:02:28.6664158 +0800 CST m=+0.012997601
2021-08-24 14:02:31.670071 +0800 CST m=+3.016652801

We can see that the running result is basically the same as the purpose we want to achieve. After the three-second timer is created, the time can only be read out after blocking for three seconds.

Let's take a look at this

x := <- 

According to the following code, this C is a one-way read-only pipeline:

type Timer struct {
    C <-chan Time
    r runtimeTimer
}

If you want to describe a one-way write-only pipeline, you should write it like this:

C chan <- Time

But if we want to achieve the same goal, we can use the following simpler method:

func main() {
    (())
    x := <- (3*)
    (x)
}

Use() to wait for a specified period of time, and then send the current time on the returned pipeline. It is equivalent to NewTimer(d).C. The garbage collector will not recycle the underlying Timer until the timer triggers. If efficiency needs to be considered, use NewTimer instead and call it when the timer is no longer needed to end.

Of course, we can also use the following method, both methods can be:

x := <- (3 * ).C

The fixed-timer just now was a time bomb that exploded after three seconds. Now let’s take a look at the periodic timer!

func main() {
    ticker := (1 * )
​
    var i int
    for{
        x := &lt;- 
        ("\r",x)
        i++
        if i&gt;3{
            //Stop the stopwatch will cause the data to never be read.            //Be sure to read it will cause deadlock.            ()
            break
        }
    }
    ("Time End")
}

The meaning of this code is to set a periodic timer, then read the data from the pipeline every second, and then output until i>3, then use() to end the timer, then stop the loop, and then tell you that the timing is over.

If you still have to persist in reading after the timer is finished, the following situation will occur!

fatal error: all goroutines are asleep - deadlock!

Deadlock appears! So break is needed here.

This is the end of this article about Go's fixed-timer and periodic timer. For more related Go timers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!