Files
bulma/gomaxprocs_demo.go
T

35 lines
691 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
func main() {
runtime.GOMAXPROCS(1)
var wg sync.WaitGroup
startTime := time.Now()
// 4个CPU密集型任务(不释放P
for i := 0; i < 4; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
taskStart := time.Now()
// 模拟1秒CPU密集型计算(不释放P)
end := time.Now().Add(1 * time.Second)
for time.Now().Before(end) {
// 空循环,持续占用CPU
}
taskDuration := time.Since(taskStart)
fmt.Printf("任务%d:自身耗时:%v,完成时间:%v\n",
id, taskDuration, time.Since(startTime))
}(i)
}
wg.Wait()
fmt.Printf("总耗时:%v\n", time.Since(startTime))
}