-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runtime.go
57 lines (53 loc) · 965 Bytes
/
test_runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// // 让出cpu时间片,重新等待安排任务
// package main
// import (
// "fmt"
// "runtime"
// )
// func show(msg string) {
// for i := 0; i < 2; i++ {
// fmt.Printf("msg:%v\n", msg)
// }
// }
//
// func main() {
// go show("java")
// for i := 0; i < 2; i++ {
// runtime.Gosched() //我有权利执行任务了,让给你(其他子协程执行)
// fmt.Printf("golang :%v\n","golang")
// }
// }
package main
import (
"fmt"
"runtime"
"time"
)
func a() {
for i := 0; i < 10; i++ {
fmt.Println("a:", i)
}
}
func b() {
for i := 0; i < 10; i++ {
fmt.Println("b:", i)
}
}
func show(msg string) {
for i := 0; i < 10; i++ {
fmt.Printf("%v:msg:%v\n", i, msg)
if i >= 5 {
runtime.Goexit() //退出进程
}
}
}
func main() {
// go show("java")
// time.Sleep(time.Second)
fmt.Printf("runtime.NumCPU(): %v\n", runtime.NumCPU())
runtime.GOMAXPROCS(1)
go a()
go b()
time.Sleep(time.Second * 3)
fmt.Println("end ...")
}