Skip to content

Instantly share code, notes, and snippets.

@ChenLingPeng
Created July 1, 2015 06:31
Show Gist options
  • Save ChenLingPeng/4107a73482b4dde6cdf9 to your computer and use it in GitHub Desktop.
Save ChenLingPeng/4107a73482b4dde6cdf9 to your computer and use it in GitHub Desktop.
func channalselectTest(){
runtime.GOMAXPROCS(2)
ch1 := make(chan int)
ch2 := make(chan int)
go pump(ch1,2)
go pump(ch2,5)
go suck(ch1,ch2)
time.Sleep(1e9)
}
func pump(ch chan int, step int){
for i :=0;;i+=step {
ch <- i
time.Sleep(5)
}
}
func suck(ch1, ch2 chan int){
for {
select {
case v:=<-ch1:
fmt.Printf("%d\n",v)
case v:=<-ch2:
_ = v
// fmt.Printf("receive channel 2: %d\n",v)
default:
fmt.Println("non...")
}
}
}
/*
期望输出
0
2
4
6
...
中间穿插一些non...
实际上到后面, 两个相邻的数输出虽然还是有序但是差值就都大于2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment