【Go】并发编程之 select


一、为什么需要 select ?

在某些场景下我们需要 同时从多个通道接收数据。从通道接收数据时,如果没有数据可以接收将会发生阻塞。你也许会写出如下代码使用遍历的方式来实现:

for{
    // 尝试从ch1接收值
    data, ok := <-ch1
    // 尝试从ch2接收值
    data, ok := <-ch2
    …
}

这种方式虽然可以实现从多个通道接收值的需求,但是运行性能会差很多。为了应对这种场景,Go内置了 select 关键字,可以 同时响应多个通道的操作


二、select 多路复用

select 的使用类似于 switch 语句,它有一系列 case 分支和一个默认的分支。每个 case 会对应一个通道的通信(接收或发送)过程。select 会一直等待,直到某个 case 的通信操作完成时,就会执行 case 分支对应的语句。具体格式如下:

select {
 case <-chan1:
    // 如果chan1成功读到数据,则进行该case处理语句
 case chan2 <- 1:
    // 如果成功向chan2写入数据,则进行该case处理语句
 default:
    // 如果上面都没有成功,则进入default处理流程
 }

1. select 可以同时监听一个或多个 channel,直到其中一个 channel ready

package main

import (
	"fmt"
	"time"
)

func test1(ch chan string) {
	time.Sleep(time.Second * 5)
	ch <- "test1"
}
func test2(ch chan string) {
	time.Sleep(time.Second * 2)
	ch <- "test2"
}

func main() {
	// 2个管道
	output1 := make(chan string)
	output2 := make(chan string)
	// 跑2个子协程,写数据
	go test1(output1)
	go test2(output2)
	// 用select监控
	select {
	case s1 := <-output1:
		fmt.Println("s1=", s1)
	case s2 := <-output2:
		fmt.Println("s2=", s2)
	}
}

输出结果:

s2= test2

2. 如果多个 channel 同时ready,则随机选择一个执行

package main

import (
	"fmt"
)

func main() {
	// 创建2个管道
	int_chan := make(chan int, 1)
	string_chan := make(chan string, 1)
	go func() {
		//time.Sleep(2 * time.Second)
		int_chan <- 1
	}()
	go func() {
		string_chan <- "hello"
	}()
	select {
	case value := <-int_chan:
		fmt.Println("int:", value)
	case value := <-string_chan:
		fmt.Println("string:", value)
	}
	fmt.Println("main结束")
}

输出结果:

string: hello
main结束

三、可以用于判断管道是否存满

package main

import (
	"fmt"
	"time"
)

// 判断管道有没有存满
func main() {
	// 创建管道
	output1 := make(chan string, 10)
	// 子协程写数据
	go write(output1)
	// 取数据
	for s := range output1 {
		fmt.Println("res:", s)
		time.Sleep(time.Second)
	}
}

func write(ch chan string) {
	for {
		select {    //如果通道已经存满,就会执行 default
		// 写数据
		case ch <- "hello":
			fmt.Println("write hello")
		default:
			fmt.Println("channel full")
		}
		time.Sleep(time.Millisecond * 500)
	}
}

输出结果:

write hello
res: hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
write hello
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
write hello
channel full
res: hello
...

参考链接

  1. select
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值