channle阻塞
Posted lc161616
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了channle阻塞相关的知识,希望对你有一定的参考价值。
package main import ( "fmt" "time" ) func readChan(inchan chan int, exitchan chan bool) { for { time.Sleep(time.Millisecond * 500) res, ok := <-inchan if !ok { break } fmt.Printf("读取%d ", res) } exitchan <- true close(exitchan) } func writeChan(inchan chan int) { for i := 1; i <= 50; i++ { inchan <- i fmt.Printf("写入%d ", i) } close(inchan) } func main() { inchan := make(chan int, 10) exitchan := make(chan bool, 1) go writeChan(inchan) go readChan(inchan, exitchan) for { _, ok := <-exitchan if !ok { break } } }
以上是关于channle阻塞的主要内容,如果未能解决你的问题,请参考以下文章
Java 学习笔记 - IO篇:常见的流Stream通道Channl以及相互关系