Skip to content

Instantly share code, notes, and snippets.

@xiaojay
Created June 27, 2013 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaojay/5873821 to your computer and use it in GitHub Desktop.
Save xiaojay/5873821 to your computer and use it in GitHub Desktop.
test2 for observe num of goroutine running http://v2ex.com/t/73772
ackage main
import (
"fmt"
"time"
"math/rand"
)
func get_rand_int(max int) int {
rand.Seed( time.Now().UTC().UnixNano())
return rand.Intn(max)
}
func report(c chan int) {
for {
fmt.Println("目前goroutine数目", len(c))
time.Sleep(1000*time.Millisecond)
}
}
func routine(c chan int){
c <- 1
n := get_rand_int(10000)
time.Sleep(time.Duration(n) * time.Millisecond)
<-c
}
func main() {
c := make(chan int, 1000000)
go report(c)
time.Sleep(1000*time.Millisecond)
for{
time.Sleep(500*time.Millisecond)
go routine(c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment