Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. "sync"
  8. "runtime"
  9. )
  10.  
  11. var wg sync.WaitGroup
  12.  
  13. func Sleep() {
  14. defer wg.Done()
  15.  
  16. r := rand.Intn(3);
  17. if r == 0 {
  18. time.Sleep(10 * time.Millisecond) //0.01秒停止
  19. } else if r == 1 {
  20. time.Sleep(100 * time.Millisecond) //0.1秒停止
  21. } else if r == 2 {
  22. time.Sleep(3000 * time.Millisecond) //3秒停止
  23. }
  24. }
  25.  
  26. func main() {
  27. N := 8;
  28. runtime.GOMAXPROCS(N)
  29.  
  30. idx := 0;
  31. for i:=0; i<18; i++ {
  32. fmt.Println("process:", i)
  33. wg.Add(1)
  34. idx += 1;
  35. go Sleep();
  36. if idx%N == 0 {
  37. wg.Wait();
  38. }
  39. }
  40.  
  41. wg.Wait();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement