我正在做一些实验。下面是我的代码。
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
答案 0 :(得分:2)
您正在向该通道写入/发送数据,但您不是从同一通道读取/接收数据。除非您从该通道读取/写入数据,否则对通道的写入/读取会阻塞。
编写一个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
}