Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/rand"
- "time"
- "sync"
- "runtime"
- )
- var wg sync.WaitGroup
- func Sleep() {
- defer wg.Done()
- r := rand.Intn(3);
- if r == 0 {
- time.Sleep(10 * time.Millisecond) //0.01秒停止
- } else if r == 1 {
- time.Sleep(100 * time.Millisecond) //0.1秒停止
- } else if r == 2 {
- time.Sleep(3000 * time.Millisecond) //3秒停止
- }
- }
- func main() {
- N := 8;
- runtime.GOMAXPROCS(N)
- idx := 0;
- for i:=0; i<18; i++ {
- fmt.Println("process:", i)
- wg.Add(1)
- idx += 1;
- go Sleep();
- if idx%N == 0 {
- wg.Wait();
- }
- }
- wg.Wait();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement