标签:pre 多核 for ocs 技术 图片 png 协程 退出
package main import ( "fmt" "runtime" ) func main(){ go func() { for i:=0;i<5;i++{ fmt.Println("go routine") } }() for j:=0;j<2;j++{ runtime.Gosched() //如果不加这句,那么在主协程执行完毕,子协程还没来得及执行,程序便结束了 fmt.Println("main routine") } /* go routine go routine go routine go routine go routine main routine main routine */ }
package main import ( "fmt" "time" ) func test(){ defer fmt.Println("ccc") return fmt.Println("ddd") } func main(){ go func() { fmt.Println("aaa") test() fmt.Println("bbb") }() time.Sleep(time.Second) /* aaa ccc bbb */ }
package main import ( "fmt" "runtime" "time" ) func test(){ defer fmt.Println("ccc") runtime.Goexit() fmt.Println("ddd") } func main(){ go func() { fmt.Println("aaa") test() fmt.Println("bbb") }() time.Sleep(time.Second) /* aaa ccc */ }
package main import ( "fmt" "runtime" ) func main(){ runtime.GOMAXPROCS(1)//表示使用单核 for { go func() { fmt.Print(1) }() fmt.Print(0) } }
package main import ( "fmt" "runtime" ) func main(){ runtime.GOMAXPROCS(4)//使用4核 for { go func() { fmt.Print(1) }() fmt.Print(0) } }
标签:pre 多核 for ocs 技术 图片 png 协程 退出
原文地址:https://www.cnblogs.com/traditional/p/9623587.html