Guest User

Untitled

a guest
Oct 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "time"
  5. )
  6.  
  7. func main() {
  8. i := 0
  9. c1 := make(chan int, 1)
  10. c2 := make(chan int, 1)
  11.  
  12. for {
  13.  
  14. for j := 0; j < 5; j++ {
  15. if j != i && j != i+2 {
  16. println(j, "is thinking")
  17. }
  18. }
  19. go philosopher(i, c1)
  20. go philosopher(i+2, c2)
  21.  
  22. <-c1
  23. <-c2
  24.  
  25. i++
  26. i = i % 5
  27. }
  28. }
  29.  
  30. func philosopher(id int, out chan<- int) {
  31. println(id, "is eating")
  32. time.Sleep(time.Millisecond * 1000)
  33. out <- 1
  34. }
Add Comment
Please, Sign In to add comment