Advertisement
lpuarmy

Dining Philo | SO

Jun 2nd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.IOException;
  3.  
  4. public class DiningPhilosophersUsingMonitor {
  5.     private static int NUMPHILOSOPHERS = 0;
  6.  
  7.     static Scanner input = new Scanner(System.in);
  8.     public static void main (String[] args) {
  9.         try {
  10.             System.out.println(" ============== Dining Philosophers ==============");
  11.             System.out.print(" Jumlah philosopher : "); NUMPHILOSOPHERS = input.nextInt();
  12.             System.out.println(" =================================================");
  13.             System.out.println(" ! Tekan Enter untuk berhenti saat proses!\n");
  14.            
  15.             Philosopher5[] philosophers = new Philosopher5[NUMPHILOSOPHERS];
  16.             PhilosopherMonitor monitor = new PhilosopherMonitor(NUMPHILOSOPHERS);
  17.            
  18.             for (int i = 0; i < NUMPHILOSOPHERS; i++) {
  19.                 philosophers[i] = new Philosopher5(i, monitor);
  20.                 new Thread(philosophers[i]).start();
  21.             }
  22.  
  23.             System.in.read();
  24.             System.exit(0);
  25.         } catch (IOException ex) {}
  26.     }
  27.  
  28. }
  29.  
  30. class Philosopher5 implements Runnable {
  31.     private Random numGenerator = new Random();
  32.     private int id;
  33.     private PhilosopherMonitor monitor;
  34.  
  35.     public Philosopher5 (int id, PhilosopherMonitor monitor) {
  36.         this.id = id;
  37.         this.monitor = monitor;
  38.     }
  39.     public void run() {
  40.         try {
  41.             while (true) {
  42.                 think();
  43.                 monitor.pickUpChopsticks(id);
  44.                 eat();
  45.                 monitor.putDownChopsticks(id);
  46.             }
  47.         } catch (InterruptedException e) {
  48.             System.out.println(" x Philosopher " + id + " diganggu");          
  49.         }
  50.     }
  51.     private void think() throws InterruptedException {
  52.         System.out.println(" ! Philosopher " + id + " berpikir");
  53.         System.out.flush();
  54.         Thread.sleep (1000);
  55.     }
  56.     private void eat() throws InterruptedException {
  57.         Thread.sleep (1000);
  58.     }
  59.    
  60. }
  61.  
  62. class PhilosopherMonitor {
  63.     private enum State {THINKING, HUNGRY, EATING};
  64.     private State[] philosopherState;
  65.  
  66.     public PhilosopherMonitor (int numPhilosophers) {
  67.         philosopherState = new State[numPhilosophers];
  68.         for (int i = 0; i < philosopherState.length; i++) {
  69.             philosopherState[i] = State.THINKING;
  70.         }
  71.     }
  72.     public synchronized void pickUpChopsticks(int philosopherId) throws InterruptedException {
  73.         philosopherState[philosopherId] = State.HUNGRY;
  74.         System.out.println(" + Philosopher " + philosopherId + " lapar");
  75.         System.out.flush();
  76.        
  77.         while (someNeighborIsEating(philosopherId)) {
  78.             wait();
  79.         }
  80.        
  81.         philosopherState[philosopherId] = State.EATING;
  82.         System.out.println(" - Philosopher " + philosopherId + " makan");
  83.         System.out.flush();
  84.     }
  85.     private boolean someNeighborIsEating(int philosopherId) {
  86.         if (philosopherState[(philosopherId + 1) % philosopherState.length] == State.EATING){
  87.             return true;
  88.         }
  89.         if (philosopherState[(philosopherId + philosopherState.length - 1) % philosopherState.length] == State.EATING){
  90.             return true;
  91.         }
  92.         return false;
  93.     }
  94.     public synchronized void putDownChopsticks(int philosopherId) {
  95.         philosopherState[philosopherId] = State.THINKING;
  96.         notifyAll();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement