I'll take a crack at it. The behavior you see is one tick about 100ms after 
start, a second one marked as 200ms after start, then one 1200ms, one at 
2200, and another at 3200ms after start.

The key is in the documentation for NewTicke 
<https://golang.org/pkg/time/#NewTicker>r: "It adjusts the intervals or 
drops ticks to make up for slow receivers."
If you add a bit more info to your app it becomes clearer: 
https://play.golang.org/p/gHZXgyIuYw7
Here is what is happening:

100ms - The ticker fires and sends a tick timestamped 100ms.
100ms - Your goroutine wakes, receives the tick timestamped 100ms.
100+ms - Your goroutine goes to sleep at line 30 (for one second).
200ms - The ticker fires again, but your goroutine is asleep.
300ms - Since the last tick has not been processed, the ticker waits, as 
described in the docs: "It adjusts the intervals or drops ticks to make up 
for slow receivers."
1100ms - Your goroutine wakes, receives the tick timestamped 200ms.
1100+ms Your goroutine goes to sleep at line 30 (for one second).
1200ms - Since the tick was processed the ticker sends its next tick.
1300ms - Since the last tick has not been processed, the ticker waits, as 
described in the docs: "It adjusts the intervals or drops ticks to make up 
for slow receivers."
2100ms - Your goroutine wakes, receives the tick timestamped 1200ms
2100ms - Your goroutine goes to sleep at line 30 (for one second).
2200ms - Since the tick was processed the ticker sends its next tick.
2300ms - Since the last tick has not been processed, the ticker waits, as 
described in the docs: "It adjusts the intervals or drops ticks to make up 
for slow receivers."

It continues on like that until the program exits after 3600ms. 

Hope this helps.

On Monday, May 25, 2020 at 12:14:24 AM UTC-4, Kai Zhang wrote:
>
> Hello,
>       I'm using ticker to do some batch works.But I found why tickers runs 
> not as expect.why the second ticker is 100 Millisecond.code is here 
> <https://play.golang.org/p/18o0wIAPRzP>
> thanks,
> zhangkai
> 2020-05-25
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/cd72e6fd-b673-4573-bf12-4171359bbff6%40googlegroups.com.

Reply via email to