当前位置:网站首页>使用 select 切换协程
使用 select 切换协程
2022-06-11 23:51:00 【InfoQ】
selectswitchselectselect {
case u:= <- ch1:
...
case v:= <- ch2:
...
...
default: // no value ready to be received
...
}
defaultbreakreturnselect- 如果都阻塞了,会等待直到其中一个可以处理
- 如果多个可以处理,随机选择一个
- 如果没有通道操作可以处理并且写了
default语句,它就会执行:default永远是可运行的(这就是准备好了,可以执行)。
selectdefaultdefaultselectbreak程序示例
package main
import (
"fmt"
"time"
)
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
go pump1(ch1)
go pump2(ch2)
go suck(ch1, ch2)
time.Sleep(1e9)
}
func pump1(ch chan int) {
for i := 0; ; i++ {
ch <- i * 2
}
}
func pump2(ch chan int) {
for i := 0; ; i++ {
ch <- i + 5
}
}
func suck(ch1, ch2 chan int) {
for {
select {
case v := <-ch1:
fmt.Printf("Received on channel 1: %d\n", v)
case v := <-ch2:
fmt.Printf("Received on channel 2: %d\n", v)
}
}
}
goroutine_select.goch1ch2pump1()pump2()suck()ch1ch2pump1()pump2()suck()selectch1ch2Received on channel 2: 148120
Received on channel 2: 148121
Received on channel 2: 148122
Received on channel 2: 148123
Received on channel 2: 148124
Received on channel 2: 148125
Received on channel 2: 148126
Received on channel 1: 296784
Received on channel 2: 148127
Received on channel 2: 148128
Received on channel 2: 148129
Received on channel 1: 296786
Received on channel 1: 296788
边栏推荐
- Binary sort tree
- 多路查找树
- How to achieve fair and equitable data access and service ecology?
- Jetpack architecture component learning (3) -- activity results API usage
- 免费分享1个新媒体运营必备的宝藏网站
- (simple statistics) acwing 3404 Who are your potential friends
- 【BBC learningenglish】with Tango
- sonarqube介紹和安裝步驟
- Beginner JS BOM implementation window centered
- RF中使用reuqests的两种方式
猜你喜欢

Read 5g RF terminal industry

2022 high voltage electrician test question simulation test question bank and online simulation test

Teach you to play with SSM framework

Solr之基础讲解入门

Lake Shore—SuperTran-VP 连续流低温恒温器系统

SAP SD create / modify price list

Jenkins of the integrate tool

Binary sort tree

2022 safety officer-b certificate theoretical question bank and simulation test

Jetpack architecture component learning (3) -- activity results API usage
随机推荐
How to construct PostgreSQL error codes
Wake up wrist - neural network and deep learning (tensorflow application) updating
chisel环境搭建(win10 + vscode)
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
Introduction aux bases de SOLR
SF14 | supertrend "super trend" indicator magic change and upgrade (source code)
Read the logstash principle
平衡二叉树(AVL 树)
Altium designer工程下多个原理图和PCB图的一一对应
2022 safety officer-b certificate theoretical question bank and simulation test
Setting alias alias and @ reference note
oracle中dblink操作
I2C read / write process
Meet o & M (I) common questions for O & M interview
Read 5g RF terminal industry
Les produits antigéniques entrent dans la famille et les entreprises chinoises de dispositifs médicaux accueillent un nouvel océan bleu
删除收货地址【项目 商城】
MySQL 8.0 decompressed version installation tutorial
One to one correspondence of multiple schematic diagrams and PCB diagrams under Altium designer project
MySQL some simple commands