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; }
public boolean pickUp() { pause(); if (!left.pickUp()) { return false; } pause(); if (!right.pickUp()) { left.putDown(); return false; } pause(); return true; }
@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(); } }
private void eat() throws InterruptedException { numberOfMeals++; comments.setText("Philosopher# " + id + " Eating"); comments.setForeground(Color.GREEN); meal.setText("Meal # :" + numberOfMeals); meal.setForeground(Color.BLUE); philLabel.setIcon(DiningPhilosophers.philEatingIcons[id]); Thread.sleep(randomPeriod.nextInt(maxPeriod)); leftChopstick.putDown(); rightChopstick.putDown(); DiningPhilosophers.permissions.release(); DiningPhilosophers.philIsEating[id] = false; }
public void run() { try { while (!Thread.interrupted()) { print(this + " " + "thinking"); pause(); // Philosopher becomes hungry print(this + " " + "grabbing right"); right.take(); print(this + " " + "grabbing left"); left.take(); print(this + " " + "eating"); pause(); right.drop(); left.drop(); } } catch (InterruptedException e) { print(this + " " + "exiting via interrupt"); } }
@Override public void run() { // TODO 自动生成的方法存根 try { while (!Thread.interrupted()) { print(this + " " + "thinking"); pause(); print(this + " " + "grabbing right"); right.take(); print(this + " " + "grabbing left"); left.take(); print(this + " " + "eating"); pause(); right.drop(); left.drop(); } } catch (InterruptedException e) { print(this + " " + "exiting via interrupt"); } }
/** https://github.com/akka/akka/tree/master/akka-samples/akka-sample-fsm-java-lambda */ public static void main(String[] args) { ActorSystem system = ActorSystem.create("example"); ActorRef chopstick1 = system.actorOf(Chopstick.props(), "Chopstick1"); ActorRef chopstick2 = system.actorOf(Chopstick.props(), "Chopstick2"); ActorRef chopstick3 = system.actorOf(Chopstick.props(), "Chopstick3"); ActorRef chopstick4 = system.actorOf(Chopstick.props(), "Chopstick4"); ActorRef chopstick5 = system.actorOf(Chopstick.props(), "Chopstick5"); ActorRef hakker1 = system.actorOf(Hakker.props(chopstick1, chopstick2), "Hakker1"); ActorRef hakker2 = system.actorOf(Hakker.props(chopstick2, chopstick3), "Hakker2"); ActorRef hakker3 = system.actorOf(Hakker.props(chopstick3, chopstick4), "Hakker3"); ActorRef hakker4 = system.actorOf(Hakker.props(chopstick4, chopstick5), "Hakker4"); ActorRef hakker5 = system.actorOf(Hakker.props(chopstick5, chopstick1), "Hakker5"); hakker1.tell(Protocol.Think, ActorRef.noSender()); hakker2.tell(Protocol.Think, ActorRef.noSender()); hakker3.tell(Protocol.Think, ActorRef.noSender()); hakker4.tell(Protocol.Think, ActorRef.noSender()); hakker5.tell(Protocol.Think, ActorRef.noSender()); }
@Override public void run() { while (feedCount != MAX_FEEDS) { synchronized (this) { boolean ready = leftChopstick.getBothChopsticks(name, rightChopstick); if (ready) { // System.out eat(); } else { starvingCount++; } } think(); } System.out.println( String.format("%s complete dinner, startving %s times", name, starvingCount)); }
public void putDown() { left.putDown(); right.putDown(); }