golang中的定时器是使用的chanel阻塞来实现的,主要使用到了time包中的内容,如果有多个定时器的channel,为了防止阻塞,可以使用select来获取遍历channel

定时器获取的channel是个单通道channel,只能读不能写,定义时这样来定义var test <-chan int

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.NewTicker(time.Second)
    t1 := time.NewTicker(time.Second * 2)
    for {
        select {
        case v := <-t.C:
            fmt.Println("hello", v)
        case v := <-t1.C:
            fmt.Println("tsh", v)
        }
    }

    // var test <-chan int
    // <-test
}

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/taoshihan/p/12023641.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程