我正在做一些实验。下面是我的代码。

package main

import (
  "fmt"
  "time"
)

func main(){
  var a chan int
  a = make(chan int)
  for j:=0;j<10;j++{
    firstRoutine(a, j)
    time.Sleep(3 * time.Millisecond)
  }
}

func firstRoutine(chennel chan int, j int){
  i:=0
  fmt.Println("j = ", j, "chennel = ", chennel)
  chennel <- i
}

输出:
F:\Git\GitHub\GO\Prog\Concorrency>go run Channels.go
j =  0 chennel =  <nil>
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send (nil chan)]:
main.firstRoutine(0x0, 0x0)
        F:/Git/GitHub/GO/Prog/Concorrency/Channels.go:24 +0x11f
main.main()
        F:/Git/GitHub/GO/Prog/Concorrency/Channels.go:14 +0x3f
exit status 2

我的程序中没有gorutines,但仍然收到此错误。

下面的程序工作正常,至少没有goroutine错误。唯一的变化是go firstRoutine(a, j)
package main

import (
  "fmt"
  "time"
)

func main(){

  var a chan int
  a = make(chan int)
  for j:=0;j<10;j++{
    go firstRoutine(a, j)
    time.Sleep(3 * time.Millisecond)
  }
}

func firstRoutine(chennel chan int, j int){
  i:=0
  fmt.Println("j = ", j, "chennel = ", chennel)
  chennel <- i
}

输出:
F:\Git\GitHub\GO\Prog\Concorrency>go run Channels.go
j =  0 chennel =  0xc000048060
j =  1 chennel =  0xc000048060
j =  2 chennel =  0xc000048060
j =  3 chennel =  0xc000048060
j =  4 chennel =  0xc000048060
j =  5 chennel =  0xc000048060
j =  6 chennel =  0xc000048060
j =  7 chennel =  0xc000048060
j =  8 chennel =  0xc000048060
j =  9 chennel =  0xc000048060

最佳答案

您正在向该通道写入/发送数据,但不是从同一通道读取/接收数据。除非您从其中读取/写入数据,否则对通道的写入/读取会阻塞。

编写for循环,以从通道a读取数据或使用缓冲通道

添加一些日志可以揭示第二种情况。

在第二种情况下,您生成了10个单独的go例程,但是所有这些例程都被阻塞了,因为从不从通道a读取数据,并且在10次迭代之后,for循环退出,其后是main函数。

package main

import (
  "fmt"
  "time"
)

func main(){

  var a chan int
  a = make(chan int)
  for j:=0;j<10;j++{
    go firstRoutine(a, j)
    fmt.Println("sleeping")
    time.Sleep(3 * time.Millisecond)
    fmt.Println("awake")
  }
}

func firstRoutine(chennel chan int, j int){
  i:=0
  fmt.Println("j = ", j, "chennel = ", chennel)
  chennel <- i
  fmt.Println("pushed to channel"); // never gets printed
}

使用缓冲通道时:
package main

import (
  "fmt"
  "time"
)

func main(){

  var a chan int
  a = make(chan int, 11) // making a buffered channel of 11 elements
  for j:=0;j<10;j++{
    go firstRoutine(a, j)
    fmt.Println("sleeping")
    time.Sleep(3 * time.Millisecond)
    fmt.Println("awake")
  }
}

func firstRoutine(chennel chan int, j int){
  i:=0
  fmt.Println("j = ", j, "chennel = ", chennel)
  chennel <- i
  fmt.Println("pushed to channel"); // gets printed as buffer is bigger than the iterations, so no blocking
}

关于go - 没有任何go例程,但仍然得到“所有goroutine都在 sleep -死锁”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58158087/

10-10 20:50