public void hungry() throws InterruptedException {
   while (DiningPhilosophers.leftNeighbourPhilosopher(id)
       || DiningPhilosophers.rightNeighbourPhilosopher(id)) {
     Thread.sleep(randomPeriod.nextInt(10));
   }
   DiningPhilosophers.permissions.acquire();
   leftChopstick.pickUp();
   rightChopstick.pickUp();
   DiningPhilosophers.philIsEating[id] = true;
 }
Beispiel #2
0
 public boolean pickUp() {
   pause();
   if (!left.pickUp()) {
     return false;
   }
   pause();
   if (!right.pickUp()) {
     left.putDown();
     return false;
   }
   pause();
   return true;
 }
Beispiel #3
0
 @Override
 public void run() {
   while (running) {
     System.out.println("Philosopher #" + getID() + " is thinking...");
     sleep(rand(50, 150));
     System.out.println("Philosopher #" + getID() + " is hungry...");
     left.pickUp(this);
     sleep(rand(5, 15));
     right.pickUp(this);
     System.out.println("Philosopher #" + getID() + " is eating...");
     sleep(rand(50, 150));
     right.drop();
     sleep(rand(5, 15));
     left.drop();
   }
 }