是否有可能發生: 假設我有3個併發例程可以將整數發送給對方。現在,假設兩個併發例程都向併發例程1發送一個整數。例程1是否可能取得兩個值並將其處理得更遠?要清楚,我有以下代碼:Google Go語言中的併發例程
package main
import "rand"
func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) {
for i := 0; i < 10; i++ {
i := rand.Intn(100)
if i%2 == 0 {
command12 <- i
}
if i%2 != 0 {
command13 <- i
}
print(<-response13, " 1st\n");
}
close(command12)
}
func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
for i := 0; ; i++ {
x, open := <-command12
if !open {
return;
}
print(x , " 2nd\n");
y := rand.Intn(100)
if i%2 == 0 {
command12 <- y
}
if i%2 != 0 {
command23 <- y
}
}
}
func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
for i := 0; ; i++ {
x, open := <-command13
if !open {
return;
}
print(x , " 3nd\n");
y := rand.Intn(100)
response23 <- y
}
}
func main() {
command12 := make(chan int)
response12 := make(chan int)
command13 := make(chan int)
response13 := make(chan int)
command23 := make(chan int)
response23 := make(chan int)
go Routine1(command12, response12,command13, response13)
Routine2(command12, response12,command23, response23)
Routine3(command13, response13,command23, response23)
}
在這裏,在這個示例程序1可以發送一個int常規2或3。我想這是例行3.現在假設,常規3還發送一個int到例程2.例程2是否有可能採用這兩個值並進一步處理(動態併發例程)?任何機構都可以幫助相應地修改這個程序。
可能重複(http://stackoverflow.com/questions/8232422/concurrent -outoutines-in-go) –