Go-goroutine

goroutine

package main

import (
	"fmt"
	"strconv"
	"time"
)

func test() {
	for i := 1; i <= 10; i++ {
		fmt.Println("test () hello world" + strconv.Itoa((i)))
		time.Sleep(time.Second)
	}
}

func main() {
	go test()
	for i := 1; i <= 10; i++ {
		fmt.Println("main () hello world" + strconv.Itoa((i)))
		time.Sleep(time.Second)
	}
}

image

设置Golang运行的cpu数

package main

import (
	"fmt"
	"runtime"
)

func main() {
	cpuNum := runtime.NumCPU()
	fmt.Println("cpuNum=", cpuNum)
	runtime.GOMAXPROCS(cpuNum - 1)
	fmt.Println("OK")
}

image

posted @ 2022-06-13 10:47  司砚章  阅读(23)  评论(0编辑  收藏  举报