From 10e9ef931bd1f037192a760f566f5bbd8e900f2b Mon Sep 17 00:00:00 2001 From: Jian Han Date: Sat, 28 Apr 2018 14:08:44 +1000 Subject: [PATCH] list thread --- master_concurrent_go/ch01/main.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/master_concurrent_go/ch01/main.go b/master_concurrent_go/ch01/main.go index 1ab765d..38df850 100644 --- a/master_concurrent_go/ch01/main.go +++ b/master_concurrent_go/ch01/main.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "runtime" + "strconv" "time" ) @@ -39,17 +40,24 @@ func main() { go outputText(hello) go outputText(world) goSched() + runtime.GOMAXPROCS(2) + fmt.Printf("%d thread(s) available to Go.", listThreads()) } func goSched() { iterations := 10 for i := 0; i <= iterations; i++ { - go showNumber(i) + showNumber(i) } - runtime.Gosched() fmt.Println("Goodbye!") } func showNumber(num int) { - fmt.Println(num) + tstamp := strconv.FormatInt(time.Now().UnixNano(), 10) + fmt.Println(num, tstamp) +} + +func listThreads() int { + threads := runtime.GOMAXPROCS(0) + return threads }