Created
June 27, 2013 03:54
-
-
Save xiaojay/5873816 to your computer and use it in GitHub Desktop.
test for observe num of goroutine running
http://v2ex.com/t/73772#reply1
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" | |
"time" | |
"math/rand" | |
"runtime" | |
) | |
func get_rand_int(max int) int { | |
rand.Seed( time.Now().UTC().UnixNano()) | |
return rand.Intn(max) | |
} | |
func report() { | |
for { | |
fmt.Println("目前goroutine数目", runtime.NumGoroutine()) | |
time.Sleep(1000*time.Millisecond) | |
} | |
} | |
func routine(){ | |
n := get_rand_int(10000) | |
time.Sleep(time.Duration(n) * time.Millisecond) | |
} | |
func main() { | |
go report() | |
for{ | |
time.Sleep(500*time.Millisecond) | |
go routine() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment