网站建设 小白,郑州网站制作专业乐云seo,wordpress主题 v7,wordpress 评论头像不显示1、Go语言并发之Select多路选择操作符
select 是类 UNIX 系统提供的一个多路复用系统 API#xff0c;Go 语言借用多路复用的概念#xff0c;提供了 select 关键字#xff0c;用
于多路监听多个通道。当监听的通道没有状态是可读或可写的#xff0c;select 是阻塞的#…1、Go语言并发之Select多路选择操作符
select 是类 UNIX 系统提供的一个多路复用系统 APIGo 语言借用多路复用的概念提供了 select 关键字用
于多路监听多个通道。当监听的通道没有状态是可读或可写的select 是阻塞的只要监听的通道中有一个状态
是可读或可写则 select 就不会阻寒而是进入处理就绪通道的分支流程。如果监听的通道有多个口读或口写
的状态则 select 随利选取一个处理。
package mainfunc main() {ch : make(chan int, 1)go func(chan int) {for {select {// 0或者1写入是随机的case ch - 0:case ch - 1:}}}(ch)for i : 0; i 10; i {println(-ch)}}# 程序结果
1
1
1
1
0
0
0
0
1
11.1 多路选择操作符 select
在golang语言中select 语句就是用来监听和channel有关的IO操作当IO操作发生时触发相应的case动作。
有了select语句可以实现 main 主线程与 goroutine 线程之间的互动。
select使用时类似 switch-case 的用法适用于处理多通道的场景会通过类似 are-you-ready-polling 的机制
来工作。
select {case -ch1 : // 检测有没有数据可读// 一旦成功读取到数据则进行该case处理语句case ch2 - 1 : // 检测有没有数据可写// 一旦成功向ch2写入数据则进行该case处理语句default:// 如果以上都没有符合条件那么进入default处理流程
}select 语句只能用于 channel 信道的IO操作每个 case 都必须是一个信道。 如果不设置 default 条件当没有IO操作发生时select 语句就会一直阻塞。 如果有一个或多个IO操作发生时Go运行时会随机选择一个 case 执行但此时将无法保证执行顺序。 对于 case 语句如果存在信道值为 nil 的读写操作则该分支将被忽略可以理解为相当于从select语句中 删除了这个case 对于空的 select 语句会引起死锁 对于在 for中的select语句不能添加 default否则会引起cpu占用过高的问题 随机性多个 case 之间并非顺序的遵循「先到先执行同时到则随机执行」的原则。 一次性和 switch-case 一样select-case也只会执行一次如果需要多次处理需要在外层套一个循环。 default 不会阻塞会一直执行当与 for 循环组合使用时可能出现死循环。
1.2 阻塞与非阻塞 select
select 默认是阻塞的当没有 case 处于激活状态时会一直阻塞住极端的甚至可以这样用
package mainfunc main() {select {// 啥也不干一直阻塞住}
}执行后引发死锁打印如下
# 输出
fatal error: all goroutines are asleep - deadlock!goroutine 1 [select (no cases)]:通过增加 default可以实现非阻塞的 select
select {case x, ok : -ch1:...case ch2 - y:...default:fmt.Println(default)}1.3 多 case 与 default 执行的顺序
整体流程如图所示
1.4 多个IO操作发生时case语句是随机执行的
package mainimport fmtfunc main() {// 创建一个长度带缓冲的整型通道ch1 : make(chan int, 1)// 向通道中写入数据ch1 - 1ch2 : make(chan int, 1)ch2 - 2select {case -ch1:fmt.Println(ch1 read)case -ch2:fmt.Println(ch2 read)}
}多次执行后会随机打印 ch1 read 或 ch2 read。
1.5 for中的select 引起CPU资源消耗过高
package mainimport (fmttime
)func main() {quit : make(chan bool)go func() {for {select {case -quit:fmt.Println(quit)// 使用 return 就会退出整个goroutine线程如果使用 break程序仍然在for循环中执行returndefault:fmt.Println(default)}}}()time.Sleep(3 * time.Second)quit - true // 主线程在3秒后向quit信道写入数据time.Sleep(2 * time.Second)fmt.Println(main)
}# 程序结果
default
default
default
default
default
default
default
......
......
default
default
default
default
default
quit
main在 for{} 的 select 语句中使用了 default 后线程就会无限执行 default 条件直到 quit 信道中读到数据否
则会一直在一个死循环中运行从而导致占满整个CPU资源。
在 for{} 的 select 语句中不建议使用 default 条件。
1.6 select语句的实际应用
(1)、实现 main主线程与 goroutine线程之间的交互、通信
package mainimport (bufiofmtos
)// 通过控制台输入bye,来控制main函数结束运行
func main() {quit : make(chan bool)ch : make(chan string)go func() {for {select {case name : -ch:fmt.Printf(from main msg: [%v]\n, name)if name bye {quit - true} else {quit - false}}}}()for {// 控制台输入fmt.Print(please input string: )scanner : bufio.NewScanner(os.Stdin)scanner.Scan()ch - scanner.Text()isOver : -quitif isOver {break}}fmt.Println(main over)
}# 输出
please input string: from main msg: [ttttt]
please input string: from main msg: [qqqq]
please input string: from main msg: [wwww]
please input string: from main msg: [bye]
main over(2)、超时实现
package mainimport (fmttime
)func main() {quit : make(chan bool)ch : make(chan int)go func() {for {select {case num : -ch:fmt.Println(num , num)case -time.After(5 * time.Second):fmt.Println(超时)quit - true}}}()for i : 0; i 2; i {ch - itime.Sleep(time.Second)}-quit // 等待超时后, 结束 main主线程fmt.Println(程序结束)
}# 输出结果
num 0
num 1
超时
程序结束1.7 select使用的区别
package mainimport (fmttime
)func server1(ch chan string) {time.Sleep(6 * time.Second)ch - from server1
}func server2(ch chan string) {time.Sleep(3 * time.Second)ch - from server2
}func main() {output1 : make(chan string)output2 : make(chan string)go server1(output1)go server2(output2)s1 : -output1fmt.Println(s1)s2 : -output2fmt.Println(s2)
}# 程序结果
from server1
from server2package mainimport (fmttime
)func server1(ch chan string) {time.Sleep(6 * time.Second)ch - from server1
}func server2(ch chan string) {time.Sleep(3 * time.Second)ch - from server2
}func main() {output1 : make(chan string)output2 : make(chan string)go server1(output1)go server2(output2)select {case s1 : -output1:fmt.Println(s1)case s2 : -output2:fmt.Println(s2)}
}# 程序结果
from server2package mainimport timeimport (fmt
)// select 管道参数并行func server1(ch chan string) {time.Sleep(time.Second * 6)ch - response from server1
}func server2(ch chan string) {time.Sleep(time.Second * 3)ch - response from server2
}func main() {output1 : make(chan string)output2 : make(chan string)go server1(output1)go server2(output2)// 管道同时readyselect随机执行// time.Sleep(time.Second)select {case s1 : -output1:fmt.Println(s1:, s1)case s2 : -output2:fmt.Println(s2:, s2)default:fmt.Println(run default)}
}# 程序结果
run defaultpackage mainimport (fmttime
)func server1(ch chan string) {ch - from server1
}func server2(ch chan string) {ch - from server2
}func main() {output1 : make(chan string)output2 : make(chan string)go server1(output1)go server2(output2)time.Sleep(1 * time.Second)select {case s1 : -output1:fmt.Println(s1)case s2 : -output2:fmt.Println(s2)}
}# 输出
from server2 和 from server1 随机交替
文章转载自: http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.baohum.com.gov.cn.baohum.com http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.tsmcc.cn.gov.cn.tsmcc.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.rykn.cn.gov.cn.rykn.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.nhzzn.cn.gov.cn.nhzzn.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn