选择通道时出现死锁


package main
import (
"fmt"
"time"
)
func Server1(ch chan string) {
time.Sleep(2*time.Second)
ch <- "from Server-1"
}
func Server2(ch chan string) {
time.Sleep(3*time.Second)
ch <- "from Server-2"
}
func main() {
input1 := make(chan string)
input2 := make(chan string)

go Server1(input1)
go Server2(input2)

for {
select {
case s1 := <- input1:
fmt.Printf(s1 + "n")
case s2 := <- input2:
fmt.Printf(s2 + "n")   
}
}
}

运行上面的代码,得到如下错误

from Server-1
from Server-2
fatal error: all goroutines are asleep - deadlock!

在这个通道选择中,我们有两个线程以不同的定时器间隔运行,为什么会出现死锁?

您启动两个goroutine,它们只在通道上发送一个值,然后它们将终止,因此main函数将单独挂起。这就是僵局的原因。

如果您修改Server1()Server2()以无休止地发送值,则不会出现死锁:

func Server1(ch chan string) {
for {
time.Sleep(2 * time.Second)
ch <- "from Server-1"
}
}
func Server2(ch chan string) {
for {
time.Sleep(3 * time.Second)
ch <- "from Server-2"
}
}

最新更新