Files
the-way-to-go_ZH_CN/eBook/14.4.md
2015-12-29 18:28:36 +08:00

125 lines
4.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 14.4 使用select切换协程
从不不同的并发执行的协程中获取值可以通过关键字`select`来完成,它和`switch`控制语句非常相似章节5.3)也被称作通信开关;它的行为像是“你准备好了吗”的轮询机制;`select`监听进入通道的数据,也可以是用通道发送值的时候。
```go
select {
case u:= <- ch1:
...
case v:= <- ch2:
...
...
default: // no value ready to be received
...
}
```
`default`语句是可选的fallthrough行为和普通的switch相似是不允许的。在任何一个case中执行`break`或者`return`select就结束了。
`select`做得就是:选择处理列出的多个通信情况中的一个。
* 如果都阻塞了,会等待直到其中一个可以处理
* 如果多个可以处理,随机选择一个
* 如果没有通道操作可以处理并且写了`default`语句,它就会执行:`default`永远是可运行的(这就是准备好了,可以执行)。
`select`中使用发送操作并且有`default`可以确保发送不被阻塞如果没有casesselect就会一直阻塞。
`select`语句实现了一种监听模式,通常用在(无限)循环中;在某种情况下,通过`break`语句使循环退出。
在程序[goroutine_select.go](examples/chapter_14/goroutine_select.go)中有2个通道`ch1``ch2`,三个协程`pump1()``pump2()``suck()`。这是一个典型的生产者消费者模式。在无限循环中,`ch1``ch2`通过`pump1()``pump2()`填充整数;`suck()`也是在无限循环中轮询输入的,通过`select`语句获取`ch1``ch2`的整数并输出。选择哪一个case取决于哪一个通道收到了信息。程序在main执行1秒后结束。
示例 14.10-[goroutine_select.go](examples/chapter_14/goroutine_select.go)
```go
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)
}
}
}
```
输出:
```
Received on channel 2: 5
Received on channel 2: 6
Received on channel 1: 0
Received on channel 2: 7
Received on channel 2: 8
Received on channel 2: 9
Received on channel 2: 10
Received on channel 1: 2
Received on channel 2: 11
...
Received on channel 2: 47404
Received on channel 1: 94346
Received on channel 1: 94348
```
一秒内的输出非常惊人如果我们给它计数goroutine_select2.go得到了90000个左右的数字。
##练习:
练习14.7
* a在练习5.4的for_loop.go中有一个常见的for循环打印数字。在函数`tel`中实现一个for循环用协程开始这个函数并在其中给通道发送数字。`main()`线程从通道中获取并打印。不要使用`time.Sleep()`来同步:[goroutine_panic.go](exercises/chapter_14/goroutine_panic.go)
* b也许你的方案有效可能会引发运行时的panic`throw:all goroutines are asleep-deadlock!` 为什么会这样?你如何解决这个问题?[goroutine_close.go]((exercises/chapter_14/goroutine_close.go))
* c解决a的另外一种方式使用一个额外的通道传递给协程然后在结束的时候随便放点什么进去。`main()`线程检查是否有数据发送给了这个通道,如果有就停止:[goroutine_select.go](exercises/chapter_14/goroutine_select.go)
练习14.8
从示例6.10的斐波那契程序开始,制定解决方案,使斐波那契周期计算独立到协程中,并可以把结果发送给通道。
结束的时候关闭通道。`main()`函数读取通道并打印结果:[goFibonacci.go](exercises/chapter_14/gofibonacci.go)
使用练习6.9中的算法写一个更短的[gofibonacci2.go](exercises/chapter_14/gofibonacci2.go)
使用`select`语句来写,并让通道退出([gofibonacci_select.go](exercises/chapter_14/gofibonacci_select.go)
注意当给结果计时并和6.10对比时,我们发现使用通道通信的性能开销有轻微削减;这个例子中的算法使用协程并非性能最好的选择;但是[gofibonacci3](exercises/chapter_14/gofibonacci3.go)方案使用了2个协程带来了3倍的提速。
练习14.9
做一个随机位生成器程序可以提供无限的随机0或者1的序列[random_bitgen.go](exercises/chapter_14/random_bitgen.go)
练习14.10[polar_to_cartesian.go](exercises/chapter_14/polar_to_cartesian.go)
这是一种综合练习使用到章节4,9,11的内容和本章内容。
## 链接
- [目录](directory.md)
- 上一节:[通道的同步:关闭通道-测试阻塞的通道](14.3.md)
- 下一节:[通道,超时和计时器](14.5.md)