package main import ( "fmt" "runtime" ) //主协程结束,其它协程也会被终止 func main(){ runtime.GOMAXPROCS(4) //设置运行程序的最大cpu核数 go func(){ for i := 0; i < 3; i++{ fmt.Println("c++") runtime.Goexit() //终止协程 } }() for i := 0; i < 2; i++{ //等待其它协程先执行完 runtime.Gosched() fmt.Println("go") } }
Go/并发/goroutine
最新推荐文章于 2024-01-18 02:11:02 发布