Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.IOException;
- public class DiningPhilosophersUsingMonitor {
- private static int NUMPHILOSOPHERS = 0;
- static Scanner input = new Scanner(System.in);
- public static void main (String[] args) {
- try {
- System.out.println(" ============== Dining Philosophers ==============");
- System.out.print(" Jumlah philosopher : "); NUMPHILOSOPHERS = input.nextInt();
- System.out.println(" =================================================");
- System.out.println(" ! Tekan Enter untuk berhenti saat proses!\n");
- Philosopher5[] philosophers = new Philosopher5[NUMPHILOSOPHERS];
- PhilosopherMonitor monitor = new PhilosopherMonitor(NUMPHILOSOPHERS);
- for (int i = 0; i < NUMPHILOSOPHERS; i++) {
- philosophers[i] = new Philosopher5(i, monitor);
- new Thread(philosophers[i]).start();
- }
- System.in.read();
- System.exit(0);
- } catch (IOException ex) {}
- }
- }
- class Philosopher5 implements Runnable {
- private Random numGenerator = new Random();
- private int id;
- private PhilosopherMonitor monitor;
- public Philosopher5 (int id, PhilosopherMonitor monitor) {
- this.id = id;
- this.monitor = monitor;
- }
- public void run() {
- try {
- while (true) {
- think();
- monitor.pickUpChopsticks(id);
- eat();
- monitor.putDownChopsticks(id);
- }
- } catch (InterruptedException e) {
- System.out.println(" x Philosopher " + id + " diganggu");
- }
- }
- private void think() throws InterruptedException {
- System.out.println(" ! Philosopher " + id + " berpikir");
- System.out.flush();
- Thread.sleep (1000);
- }
- private void eat() throws InterruptedException {
- Thread.sleep (1000);
- }
- }
- class PhilosopherMonitor {
- private enum State {THINKING, HUNGRY, EATING};
- private State[] philosopherState;
- public PhilosopherMonitor (int numPhilosophers) {
- philosopherState = new State[numPhilosophers];
- for (int i = 0; i < philosopherState.length; i++) {
- philosopherState[i] = State.THINKING;
- }
- }
- public synchronized void pickUpChopsticks(int philosopherId) throws InterruptedException {
- philosopherState[philosopherId] = State.HUNGRY;
- System.out.println(" + Philosopher " + philosopherId + " lapar");
- System.out.flush();
- while (someNeighborIsEating(philosopherId)) {
- wait();
- }
- philosopherState[philosopherId] = State.EATING;
- System.out.println(" - Philosopher " + philosopherId + " makan");
- System.out.flush();
- }
- private boolean someNeighborIsEating(int philosopherId) {
- if (philosopherState[(philosopherId + 1) % philosopherState.length] == State.EATING){
- return true;
- }
- if (philosopherState[(philosopherId + philosopherState.length - 1) % philosopherState.length] == State.EATING){
- return true;
- }
- return false;
- }
- public synchronized void putDownChopsticks(int philosopherId) {
- philosopherState[philosopherId] = State.THINKING;
- notifyAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement