dongyuan3094 2016-06-22 08:34
浏览 45
已采纳

golang行情自动收录器如何工作?

func Tick() {                                                                                                                                         
    fmt.Println("startTime", time.Now().Format("2006-01-02 15:04:05"))                                                                                
    t := time.NewTicker(time.Second * 3)                                                                                                              
    time.Sleep(time.Second * 12)                                                                                                                      
    for {                                                                                                                                             
        stamp := <-t.C                                                                                                                                
        fmt.Println("tickTime", stamp.Format("2006-01-02 15:04:05"))                                                                                  
    }                                                                                                                                                 
}          

Otput from above snippet is:

startTime 2016-06-22 16:22:20

tickTime 2016-06-22 16:22:23

tickTime 2016-06-22 16:22:35

tickTime 2016-06-22 16:22:38

tickTime 2016-06-22 16:22:41

tickTime 2016-06-22 16:22:44


Why has this happend with no timestamp 16:22:26, 16:22:29 when I delay the ticker?

  • 写回答

1条回答 默认 最新

  • dongxu8533486 2016-06-22 09:05
    关注

    This is the Ticker source (pardon the line numbers, I copied this off the documentation source page):

        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
            }
    

    Note the comment

    // 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.
    

    What's happening:

    1. You create the timer
    2. The timer produces its first tick and buffers it.
    3. Now it waits, wakes up, and blocks, waiting for you to consume so it can produce tick 2.
    4. Eventually, your goroutine wakes up and immediately consumes the first two ticks it produced, and it starts producing ticks again.

    Edit: In addition, the documention for NewTicker (which Tick is a convenience function for) says:

    NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops 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.

    Though it doesn't explicitly mention it's a channel with a buffer of one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据