这篇文章总结了channel的10种经常使用操做,以一个更高的视角看待channel,会给你们带来对channel更全面的认识。golang
在介绍10种操做前,先简要介绍下channel的使用场景、基本操做和注意事项。安全
把channel用在数据流动的地方:并发
channel存在3种状态
:less
nil
channel可进行3种操做
:异步
把这3种操做和3种channel状态能够组合出9种状况
:高并发
操做 | nil的channel | 正常channel | 已关闭channel |
---|---|---|---|
<- ch | 阻塞 | 成功或阻塞 | 读到零值 |
ch <- | 阻塞 | 成功或阻塞 | panic |
close(ch) | panic | 成功 | panic |
对于nil通道的状况,也并不是彻底遵循上表,有1个特殊场景:当nil
的通道在select
的某个case
中时,这个case会阻塞,但不会形成死锁。oop
参考代码请看:https://dave.cheney.net/2014/...spa
下面介绍使用channel的10种经常使用操做。.net
for-range
读取channel,这样既安全又便利,当channel关闭时,for循环会自动退出,无需主动监测channel是否关闭,能够防止读取已经关闭的channel,形成读到数据为通道所存储的数据类型的零值。for x := range ch{ fmt.Println(x) }
_,ok
判断channel是否关闭原理:读已关闭的channel会获得零值,若是不肯定channel,须要使用ok
进行检测。ok的结果和含义:指针
true
:读到数据,而且通道没有关闭。false
:通道关闭,无数据读到。if v, ok := <- ch; ok { fmt.Println(v) }
select
能够同时监控多个通道的状况,只处理未阻塞的case。当通道为nil时,对应的case永远为阻塞,不管读写。特殊关注:普通状况下,对nil的通道写操做是要panic的。// 分配job时,若是收到关闭的通知则退出,不分配job func (h *Handler) handle(job *Job) { select { case h.jobCh<-job: return case <-h.stopCh: return } }
用法:
// 只有generator进行对outCh进行写操做,返回声明 // <-chan int,能够防止其余协程乱用此通道,形成隐藏bug func generator(int n) <-chan int { outCh := make(chan int) go func(){ for i:=0;i<n;i++{ outCh<-i } }() return outCh } // consumer只读inCh的数据,声明为<-chan int // 能够防止它向inCh写数据 func consumer(inCh <-chan int) { for x := range inCh { fmt.Println(x) } }
// 无缓冲 ch1 := make(chan int) ch2 := make(chan int, 0) // 有缓冲 ch3 := make(chan int, 1)
func test() { inCh := generator(100) outCh := make(chan int, 10) // 使用5个`do`协程同时处理输入数据 var wg sync.WaitGroup wg.Add(5) for i := 0; i < 5; i++ { go do(inCh, outCh, &wg) } go func() { wg.Wait() close(outCh) }() for r := range outCh { fmt.Println(r) } } func generator(n int) <-chan int { outCh := make(chan int) go func() { for i := 0; i < n; i++ { outCh <- i } close(outCh) }() return outCh } func do(inCh <-chan int, outCh chan<- int, wg *sync.WaitGroup) { for v := range inCh { outCh <- v * v } wg.Done() }
select
和time.After
,看操做和定时器哪一个先返回,处理先完成的,就达到了超时控制的效果func doWithTimeOut(timeout time.Duration) (int, error) { select { case ret := <-do(): return ret, nil case <-time.After(timeout): return 0, errors.New("timeout") } } func do() <-chan int { outCh := make(chan int) go func() { // do work }() return outCh }
func unBlockRead(ch chan int) (x int, err error) { select { case x = <-ch: return x, nil case <-time.After(time.Microsecond): return 0, errors.New("read time out") } } func unBlockWrite(ch chan int, x int) (err error) { select { case ch <- x: return nil case <-time.After(time.Microsecond): return errors.New("read time out") } }
注:time.After等待能够替换为default,则是channel阻塞时,当即返回的效果
close(ch)
关闭全部下游协程ch
的协程都会收到close(ch)
的信号func (h *Handler) Stop() { close(h.stopCh) // 可使用WaitGroup等待全部协程退出 } // 收到中止后,再也不处理请求 func (h *Handler) loop() error { for { select { case req := <-h.reqCh: go handle(req) case <-h.stopCh: return } } }
chan struct{}
做为信号channel// 上例中的Handler.stopCh就是一个例子,stopCh并不须要传递任何数据 // 只是要给全部协程发送退出的信号 type Handler struct { stopCh chan struct{} reqCh chan *Request }
reqCh chan *Request // 好过 reqCh chan Request
package main import ( "fmt" "math/rand" "sync" "time" ) func main() { reqs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // 存放结果的channel的channel outs := make(chan chan int, len(reqs)) var wg sync.WaitGroup wg.Add(len(reqs)) for _, x := range reqs { o := handle(&wg, x) outs <- o } go func() { wg.Wait() close(outs) }() // 读取结果,结果有序 for o := range outs { fmt.Println(<-o) } } // handle 处理请求,耗时随机模拟 func handle(wg *sync.WaitGroup, a int) chan int { out := make(chan int) go func() { time.Sleep(time.Duration(rand.Intn(3)) * time.Second) out <- a wg.Done() }() return out }
- 若是这篇文章对你有帮助,请点个赞/喜欢,感谢。
- 本文做者:大彬
- 若是喜欢本文,随意转载,但请保留此原文连接:http://lessisbetter.site/2019/01/20/golang-channel-all-usage/