当前位置:网站首页>golang中的select详解
golang中的select详解
2022-07-01 17:49:00 【IT工作者】
注意
监听的case中,没有满足条件的就阻塞
多个满足条件的就任选一个执行
select本身不带循环,需要外层的fo
default通常不用,会产生忙轮询
break只能跳出select中的一个case
加入了默认分支,那么无论涉及通道操作的表达式是否有阻塞,select语句都不会被阻塞。如果那几个表达式都阻塞了,或者说都没有满足求值的条件,那么默认分支就会被选中并执行。
如果没有加入默认分支,那么一旦所有的case表达式都没有满足求值条件,那么select语句就会被阻塞。直到至少有一个case表达式满足条件为止。
如果select语句发现同时有多个候选分支满足选择条件,那么它就会用一种伪随机的算法在这些分支中选择一个并执行。注意,即使select语句是在被唤醒时发现的这种情况,也会这样做
作用
go里面提供了一个关键字select,通过select可以监听channel上的数据流动
select的用法与switch语言非常类似,由select开始一个新的选择块,每个选择块条件由case语句来描述
与switch语句可以选择任何可使用相等比较的条件相比,select有比较多的限制,其中最大的一条限制就是每个case语句里必须是一个IO操作
for {
select {
case <-chan1:
//.....
case chan2<-1:
//....
default:
//都没成功,进入......
}
}在一个select语句中,go语言会按顺序从头到尾评估每一个发送和接收的语句
如果其中的任意一语句可以继续执行(即没有被阻塞),那么就从哪些可以执行的语句中任意选择一条来使用
如果没有任意一条语句可以执行(即所有的通道都被阻塞),那么有两种可能的情况:
如果给出了default语句,那么就会执行default语句,同时程序的执行会从select语句后的语句中恢复
如果没有default语句,那么select语句将被阻塞,直到至少有一个通信可以进行下去
防止channel超时机制
有时候会出现协程阻塞的情况,那么我们如何避免这个情况?我们可以使用select来设置超时
func main() {
c := make(chan int)
o := make(chan bool)
go func() {
for {
select {
case v:= <-c:
fmt.Println(v)
//5秒钟自动关闭,避免长时间超时
case <-time.After(5 * time.Second):
fmt.Println("timeout")
o<-true
break
}
}
}()
//有值就主协程走,主协程走完就都没了
<-o
fmt.Println("程序结束")
}超时这种写法也常常用来设置定时执行某段函数
go func(){
select {
case <-time.After(5 * time.Second):
dosometing()
}
}
边栏推荐
- Sword finger offer II 105 Maximum area of the island
- Common design parameters of solid rocket motor
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- 中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
- Object. fromEntries()
- What are the legal risks of NFT brought by stars such as curry and O'Neill?
- PHP implements sensitive word filtering system "suggestions collection"
- (十六)ADC转换实验
- Pyqt5, draw a histogram on the control
- In depth Research Report on China's disposable sanitary products production equipment industry (2022 Edition)
猜你喜欢

Wechat applet blind box - docking wechat payment

ACL 2022 | decomposed meta learning small sample named entity recognition

People help ant help task platform repair source code

New 95 community system whole station source code

Gold, silver and four want to change jobs, so we should seize the time to make up

SQL injection vulnerability (MySQL and MSSQL features)
![[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON](/img/83/9bd9ce7608ebfe7207ac008b9e8ab1.png)
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON

(十七)DAC转换实验

Iommu/smmuv3 code analysis (10) page table operation

ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
随机推荐
Length of learning and changing
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
RadHat搭建内网YUM源服务器
Common design parameters of solid rocket motor
pyqt5中,在控件上画柱状图
Sword finger offer 20 String representing numeric value
Replace UUID, nanoid is faster and safer!
Openlayers customize bubble boxes and navigate to bubble boxes
Key points on February 15, 2022
Cookies and session keeping technology
vulnhub靶场-hacksudo - Thor
MFC obtains local IP (used more in network communication)
The latest intelligent factory MES management system software solution
Good looking UI mall source code has been scanned, no back door, no encryption
两数之和c语言实现[通俗易懂]
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
Redis -- data type and operation
MySQL + JSON = King fried
整形数组合并【JS】
Thinkphp6 - CMS multi wechat management system source code