初始化Go学习项目
This commit is contained in:
25
go-sync-practice/race_counter.go
Normal file
25
go-sync-practice/race_counter.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var wg sync.WaitGroup
|
||||
counter := 0
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < 1000; j++ {
|
||||
counter++
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
fmt.Println("期待的结果:", 1000*1000)
|
||||
fmt.Println("实际结果:", counter)
|
||||
}
|
||||
Reference in New Issue
Block a user