🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package main import ( "fmt" "sync" "time" ) func main() { const numGoroutines = 100 const iterations = 100000 var data int var mu sync.Mutex var wg sync.WaitGroup startTime := time.Now() for i := 0; i < numGoroutines; i++ { wg.Add(1) go func() { for j := 0; j < iterations; j++ { mu.Lock() data++ mu.Unlock() } wg.Done() }() } wg.Wait() endTime := time.Now() elapsedTime := endTime.Sub(startTime) fmt.Println("Final data value:", data) fmt.Println("Total time taken:", elapsedTime) } ~~~