In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve golang timing tasks time.Sleep and time.Tick". In daily operation, I believe many people have doubts about how to achieve golang timing tasks time.Sleep and time.Tick. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "golang timing tasks time.Sleep and time.Tick". Next, please follow the editor to study!
Generally speaking
Sleep uses sleep to complete the timing, and then continues to execute the cycle after the end to achieve the timing task.
The Tick function uses channel to block the current protocol and complete the execution of scheduled tasks.
Now let's take a look at the difference between the two methods.
Here we set the timing length to 5.
Using "Do Something" to simulate scheduled task execution time is divided into 1 s execution and 10 s execution
The code is as follows
Func Test_Sleep (t * testing.T) {for I: = 0; I
< 3; i++ { Debug("begin", time.Now().Format("2006-01-02_15:04:05")) Debug("Do something 1s") time.Sleep(time.Second * 1) Debug("end", time.Now().Format("2006-01-02_15:04:05")) time.Sleep(time.Second * 5) }}func Test_Tick(t *testing.T) { t1 := time.NewTicker(5 * time.Second)(5 * time.Second) for { select { case |Sleep---------->| | Do- > | Sleep- > | |
Trick result 1
16:22:09 on 2019-04-19 | DEBUG | Test_Trick () | 90 | begin 2019-04-19 July 16 purl 22 purl 09
16:22:09 on 2019-04-19 | DEBUG | Test_Trick () | 91 | Do something 1s
16:22:10 on 2019-04-19 | DEBUG | Test_Trick () | 93 | end 2019-04-19 July 16 purl 22 purl 10
16:22:14 on 2019-04-19 | DEBUG | Test_Trick () | 90 | begin 2019-04-19 July 16 purl 22 purl 14
16:22:14 on 2019-04-19 | DEBUG | Test_Trick () | 91 | Do something 1s
16:22:15 on 2019-04-19 | DEBUG | Test_Trick () | 93 | end 2019-04-19 July 16 purl 22 purl 15
16:22:19 on 2019-04-19 | DEBUG | Test_Trick () | 90 | begin 2019-04-19 16-22-19
16:22:19 on 2019-04-19 | DEBUG | Test_Trick () | 91 | Do something 1s
16:22:20 on 2019-04-19 | DEBUG | Test_Trick () | 93 | end 2019-04-19 July 16 purl 22 purl 20
Set the task execution time to 1s
The interval between end and the next begin is 4s (fixed time interval-task execution time)
The process is roughly as follows:
(begin) Do- > (end)
| | DO- >-| Do- >-| |
| | Sleep- > | Sleep- > | |
Sleep result 2:
16:32:41 on 2019-04-19 | DEBUG | Test_Sleep () | 77 | begin 2019-04-19 July 1615 32 purl 41
16:32:41 on 2019-04-19 | DEBUG | Test_Sleep () | 78 | Do something 10s
16:32:51 on 2019-04-19 | DEBUG | Test_Sleep () | 80 | end 2019-04-19 July 1615 32 purl 51
16:32:56 on 2019-04-19 | DEBUG | Test_Sleep () | 77 | begin 2019-04-19 July 1615 32 Swiss 56
16:32:56 on 2019-04-19 | DEBUG | Test_Sleep () | 78 | Do something 10s
16:33:06 on 2019-04-19 | DEBUG | Test_Sleep () | 80 | end 2019-04-19 July 16 charge 33 purl 06
Set the task execution time to 10s
The interval between end and the next begin is 5s (fixed time interval)
The process is roughly as follows:
| | DO- > | Sleep- > | Do- > | Sleep- > | |
Trick result 2
16:41:05 on 2019-04-19 | DEBUG | Test_Tick () | 90 | begin 2019-04-19 July 16 purl 41purl 05
16:41:05 on 2019-04-19 | DEBUG | Test_Tick () | 91 | Do something 10s
16:41:15 on 2019-04-19 | DEBUG | Test_Tick () | 93 | end 2019-04-19 July 16
16:41:15 on 2019-04-19 | DEBUG | Test_Tick () | 90 | begin 2019-04-19 July 16 purl 41purl 15
16:41:15 on 2019-04-19 | DEBUG | Test_Tick () | 91 | Do something 10s
16:41:25 on 2019-04-19 | DEBUG | Test_Tick () | 93 | end 2019-04-19 July 16 purl 41 purl 25
Set the task execution time to 10s
The interval between end and the next begin is 0s
At this time, because the execution time of the task is longer than the fixed time interval, when the task is finished, there is no blocking, so you can execute the next time immediately.
The process is roughly as follows:
(begin) Do- > (end)
| | DO- > | Do--- > | |
| | Sleep- >-| Sleep- >-| |
At this point, the study on "how to implement golang timing tasks time.Sleep and time.Tick" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.