Created
June 27, 2013 03:56
-
-
Save xiaojay/5873821 to your computer and use it in GitHub Desktop.
test2 for observe num of goroutine running http://v2ex.com/t/73772
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
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