The trap of a timer in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The Golang Timer class is a universal timer that has some of the features of a common timer, such as:

    • Given an expiry time, and a callback function, the callback function is called when it expires
    • Reset Timer Time-out
    • Stop Timer

Golang timer in the source code, the way to achieve is to maintain a small heap of all the timer collection. A separate goroutine is then started, looping from the top heap to detect the expiry time of the most recently expired timer, and then it sleeps to the time when the last timer expires. Finally, the callback function set at the start is executed. After the timer expires, the Golang runtime is removed from the small heap and waits for the GC to reclaim resources.

The actual code is given below:

package mainimport (    "time"    "fmt")func main() {    timer := time.NewTimer(3 * time.Second)    go func() {        <-timer.C        fmt.Println("Timer has expired.")    }()    timer.Stop()    time.Sleep(60 * time.Second)}

timer.NewTimer()Starts a new timer instance and starts the timer. We launch a new goroutine to block the way from the C channel of the timer, waiting to receive a value that is the time of expiry. and print "Timer has expired."

It doesn't seem to be a problem now, but when we do timer.Stop() , 3 seconds later, the program doesn't print that sentence. After execution timer.Stop() , the channel that the timer comes from does not close, and the timer has been removed from runtime, so the timer will never expire.

This can cause program logic errors or, more seriously, cause goroutine and memory leaks. The workaround is to use the timer.Reset() substitution timer.Stop() to stop the timer.

package mainimport (    "time"    "fmt")func main() {    timer := time.NewTimer(3 * time.Second)    go func() {        <-timer.C        fmt.Println("Timer has expired.")    }()    //timer.Stop()    timer.Reset(0  * time.Second)    time.Sleep(60 * time.Second)}

This is equivalent to giving the timer a 0-second timeout to expire the timer immediately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.