Created
November 13, 2019 13:45
-
-
Save shibukawa/9e3889627c393a040dcfddd3e00a8d25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"math/rand" | |
"runtime" | |
"time" | |
"sync/atomic" | |
) | |
func main() { | |
result := make([]int, 1000) | |
m := make(map[int]int) | |
for i := 0; i < 500; i++ { | |
m[i] = rand.Int() | |
} | |
var running int64 = 1 | |
log.Println("start running") | |
for i := 0; i < 100; i++ { | |
go func(index int) { | |
r := rand.New(rand.NewSource(int64(index))) | |
for atomic.LoadInt64(&running) == 1 { | |
result[index] = m[r.Intn(1000)] | |
} | |
} (i) | |
} | |
/*go func() { | |
r := rand.New(rand.NewSource(1001)) | |
for atomic.LoadInt64(&running) == 1 { | |
m[r.Intn(1000)] = r.Int() | |
} | |
}()*/ | |
time.Sleep(time.Second) | |
log.Println(runtime.NumGoroutine()) | |
time.Sleep(time.Second) | |
atomic.SwapInt64(&running, 0) | |
time.Sleep(time.Second) | |
log.Println(runtime.NumGoroutine()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment