zoukankan      html  css  js  c++  java
  • golang中管道热替换

    golang中管道替换问题

    https://blog.csdn.net/cyk2396/article/details/78875347

    1.运行以下代码:

    var chan1 chan int
    var chanLength int = 18
    var interval time.Duration = 1500 * time.Millisecond

    //var interval2 time.Duration = 1800 * time.Millisecond

    //golang在替换通道时引起的问题
    func main() {
    chan1 = make(chan int, chanLength)
    //该goroutine是每隔1.5秒向chan1中发送一个元素
    //当满足条件时,重置chan1,此时chan1会指向一个新通道
    go func() {
    for i := 0; i < chanLength; i++ {
    if i > 0 && i%3 == 0 {
    fmt.Println("chan1 reset")
    chan1 = make(chan int, chanLength)
    }
    fmt.Println("send an value : ", i)
    chan1 <- i
    time.Sleep(interval)
    }
    fmt.Println("close chan1")
    close(chan1)
    }()

    //用于接收chan1中的元素
    receive(chan1)
    

    }

    func receive(chan2 chan int) {
    fmt.Println("start receive value from chan1 ...")
    timer := time.After(30 * time.Second)
    Loop:
    for {
    select {
    case e, ok := <-chan2:
    if !ok {
    fmt.Println("chan1 has closed ...")
    break Loop
    }

    		fmt.Println("receive value : ", e)
    		time.Sleep(interval)
    	case &lt;-timer:
    		fmt.Println("time out")
    		break Loop
    	}
    }
    

    }
    结果如下:

    start receive value from chan1 ...
    send an value : 0
    receive value : 0
    send an value : 1
    receive value : 1
    send an value : 2
    receive value : 2
    chan1 reset
    send an value : 3
    send an value : 4
    send an value : 5
    chan1 reset
    send an value : 6
    send an value : 7
    send an value : 8
    chan1 reset
    send an value : 9
    send an value : 10
    send an value : 11
    chan1 reset
    send an value : 12
    send an value : 13
    send an value : 14
    chan1 reset
    send an value : 15
    send an value : 16
    send an value : 17
    close chan1
    time out
    引起这个的原因是:对变量的重新赋值操作是无法被传递的。在上面这个demo中,当chan1指向了新管道时,receive()函数还是调用原来的旧管道。

    解决办法是:通过函数调用每次都获取最新的管道。代码如下:

    var chan1 chan int
    var chanLength int = 18
    var interval time.Duration = 1500 * time.Millisecond

    var interval2 time.Duration = 1550 * time.Millisecond

    //golang在替换通道时引起的问题
    func main() {
    chan1 = make(chan int, chanLength)
    //该goroutine是每隔1.5秒向chan1中发送一个元素
    //当满足条件时,重置chan1,此时chan1会指向一个新通道
    go func() {
    for i := 0; i < chanLength; i++ {
    if i > 0 && i%3 == 0 {
    fmt.Println("chan1 reset")
    chan1 = make(chan int, chanLength)
    }
    fmt.Println("send an value : ", i)
    chan1 <- i
    time.Sleep(interval)
    }
    fmt.Println("close chan1")
    close(chan1)
    }()

    //用于接收chan1中的元素
    receive()
    

    }

    func receive() {
    fmt.Println("start receive value from chan1 ...")
    timer := time.After(30 * time.Second)
    Loop:
    for {
    select {
    case e, ok := <-getChan(): //通过getChan()每次都获取最新的管道
    if !ok {
    fmt.Println("chan1 has closed ...")
    break Loop
    }

    		fmt.Println("receive value : ", e)
    		time.Sleep(interval2) // 时间间隔相比发送方的时间间隔增加50ms,为了在getChan()时能获取最新的chan
    	case &lt;-timer:
    		fmt.Println("time out")
    		break Loop
    	}
    }
    

    }

    //获取管道chan1
    func getChan() chan int {
    return chan1
    }

  • 相关阅读:
    Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(一)
    模块化开发
    最近理解记忆
    ES6语法—— 模块化开发之import和export命令详解
    关于阿里图标库Iconfont生成图标的三种使用方式(fontclass/unicode/symbol)
    2.新知-display:inline-block元素之间空隙的产生原因和解决办法
    规避同源
    vue总结
    vue组件通信
    vue 多次学习
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/9263102.html
Copyright © 2011-2022 走看看