Advertisement
Guest User

Dining Philosophers

a guest
Apr 9th, 2020
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.concurrent.Semaphore;
  3.  
  4. class Demo {
  5.  
  6.     public static void main(String[] args) throws InterruptedException {
  7.         DiningPhilosophers.runTest();
  8.     }
  9. }
  10.  
  11. public class DiningPhilosophers {
  12.  
  13.     private static Random random = new Random(System.currentTimeMillis());
  14.     private Semaphore[] forks = new Semaphore[5];
  15.  
  16.     public DiningPhilosophers() {
  17.         forks[0] = new Semaphore(1);
  18.         forks[1] = new Semaphore(1);
  19.         forks[2] = new Semaphore(1);
  20.         forks[3] = new Semaphore(1);
  21.         forks[4] = new Semaphore(1);
  22.     }
  23.  
  24.     public void lifecycleOfPhilosopher(int id) throws InterruptedException {
  25.  
  26.         while (true) {
  27.             think(id);
  28.             eat(id);
  29.         }
  30.     }
  31.  
  32.     void think(int id) throws InterruptedException {
  33.         System.out.println(String.format("Philosopher %d is thinking.", id));
  34.         Thread.sleep(random.nextInt(50));
  35.     }
  36.  
  37.     void eat(int id) throws InterruptedException {
  38.         // TODO: 3/29/20 Synchronize
  39.         int leftIndex = id;
  40.         int rightIndex = (id!=0)? id-1 : 4;
  41.         System.out.println(String.format("Philosopher %d trying to pick up left fork.", id));
  42.         forks[leftIndex].acquire();
  43.         System.out.println(String.format("Philosopher %d picked up left fork.", id));
  44.         System.out.println(String.format("Philosopher %d trying to pick up right fork.", id));
  45.         if (forks[rightIndex].tryAcquire()) {
  46.             System.out.println(String.format("Philosopher %d picked up right fork.", id));
  47.             eating(id);
  48.             System.out.println(String.format("Philosopher %d putting down right fork", id));
  49.             forks[rightIndex].release();
  50.         }
  51.         System.out.println(String.format("Philosopher %d putting down left fork", id));
  52.         forks[id].release();
  53.     }
  54.  
  55.     private void eating(int id) throws InterruptedException {
  56.         System.out.println(String.format("Philosopher %d is eating", id));
  57.         Thread.sleep(random.nextInt(50));
  58.     }
  59.  
  60.     static void runPhilosopher(DiningPhilosophers dp, int id) {
  61.         try {
  62.             dp.lifecycleOfPhilosopher(id);
  63.         } catch (InterruptedException ie) {
  64.  
  65.         }
  66.     }
  67.  
  68.     public static void runTest() throws InterruptedException {
  69.         final DiningPhilosophers dp = new DiningPhilosophers();
  70.  
  71.         Thread p1 = new Thread(new Runnable() {
  72.  
  73.             public void run() {
  74.                 runPhilosopher(dp, 0);
  75.             }
  76.         });
  77.  
  78.         Thread p2 = new Thread(new Runnable() {
  79.  
  80.             public void run() {
  81.                 runPhilosopher(dp, 1);
  82.             }
  83.         });
  84.  
  85.         Thread p3 = new Thread(new Runnable() {
  86.  
  87.             public void run() {
  88.                 runPhilosopher(dp, 2);
  89.             }
  90.         });
  91.  
  92.         Thread p4 = new Thread(new Runnable() {
  93.  
  94.             public void run() {
  95.                 runPhilosopher(dp, 3);
  96.             }
  97.         });
  98.  
  99.         Thread p5 = new Thread(new Runnable() {
  100.  
  101.             public void run() {
  102.                 runPhilosopher(dp, 4);
  103.             }
  104.         });
  105.  
  106.         p1.start();
  107.         p2.start();
  108.         p3.start();
  109.         p4.start();
  110.         p5.start();
  111.  
  112.         p1.join();
  113.         p2.join();
  114.         p3.join();
  115.         p4.join();
  116.         p5.join();
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement