大佬教程收集整理的这篇文章主要介绍了为什么这个Google I / O 2012并发示例无法正常工作?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您的代码很好;要使它们不同步,往往需要花费更多。循环更多次,您应该看到它们脱离了锁步:
for i := 0; i < 20; i++ { // Going up to 20 is enough to usually see it
fmt.Println(<-C)
}
我得到以下输出:
乔0
安0
乔1
安1
乔2
安2
乔3
安3
乔4
安4
乔5
安5
乔6
安6
安7
乔7
乔8
乔9
安8
安9
我正在尝试跟Rob Pike的Google I / O 2012演讲“ Go Concurrency”一起进行。我正在尝试通道多路复用的示例,以使“
Ann”和“ Joe”不会步调一致。但是使用下面的代码,它们仍然处于锁定状态。我要去哪里错了?
视频:http
:
//www.youtube.com/watch?v=
f6kdp27TYZs&
feature=player_detailpage#
t=
1025s
package main
import (
"fmt"
"time"
"math/rand"
)
func fanIn(input1,input2 <-chan String) <-chan String {
c := make(chan String)
go func() { for {C <- <-input1 } }()
go func() { for {C <- <-input2 } }()
return c
}
func main() {
c := fanIn(boring("Joe"),boring("Ann"))
for i:=0; i<10; i++ {
fmt.Println(<-C)
}
fmt.Printf("You're both boring,I'm leaving...\n")
}
func boring(msg String) <-chan String {
c := make(chan String)
go func() { // launch goroutIne from inside the fn
for i:=0; ; i++ {
c <- fmt.Sprintf("%s %d",msg,i)
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond )
}
}()
return c
}
以及它的输出(在Ubuntu 10.04 LTS上为go1.0.2版)
Joe 0
Ann 0
Joe 1
Ann 1
Joe 2
Ann 2
Joe 3
Ann 3
Joe 4
Ann 4
You're both boring,I'm leaving...
我哪里做错了?谢谢!
以上是大佬教程为你收集整理的为什么这个Google I / O 2012并发示例无法正常工作?全部内容,希望文章能够帮你解决为什么这个Google I / O 2012并发示例无法正常工作?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。