Created
August 1, 2013 13:48
-
-
Save cloudaice/6131509 to your computer and use it in GitHub Desktop.
Go 时间驱动 协程库
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "time" | |
import "runtime" | |
func main(){ | |
runtime.GOMAXPROCS(8) | |
channel := make(chan string) | |
for i:=0; i<5; i++ { | |
go func (id int) { | |
for { | |
time.Sleep(3) | |
msg := <-channel | |
fmt.Println(msg, id) | |
} | |
}(i) | |
} | |
ticker := time.NewTicker(time.Second) | |
for t := range ticker.C { | |
fmt.Println(t) | |
channel <- "hello world" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment