Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.concurrent.Semaphore;
  3.  
  4. public class Demo {
  5.  
  6.     public static void main(String args[]) throws InterruptedException {
  7.         DiningPhilosophers.runTest();
  8.     }
  9. }
  10.  
  11. 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("%d thinking", id));
  34.         Thread.sleep(random.nextInt(50));
  35.     }
  36.  
  37.     void eat(int id) throws InterruptedException {
  38.         if (forks[id].tryAcquire()) {
  39.             if (forks[(id - 1 + 5) % 5].tryAcquire()) {
  40.                 System.out.println(id + " Eating...");
  41.             }
  42.             forks[(id - 1 + 5) % 5].release();
  43.         }
  44.         forks[id].release();
  45.         // TODO: 3/29/20 Synchronize
  46.     }
  47.  
  48.     static void runPhilosopher(DiningPhilosophers dp, int id) {
  49.         try {
  50.             dp.lifecycleOfPhilosopher(id);
  51.         } catch (InterruptedException ie) {
  52.  
  53.         }
  54.     }
  55.  
  56.     public static void runTest() throws InterruptedException {
  57.         final DiningPhilosophers dp = new DiningPhilosophers();
  58.  
  59.         Thread p1 = new Thread(new Runnable() {
  60.  
  61.             public void run() {
  62.                 runPhilosopher(dp, 0);
  63.             }
  64.         });
  65.  
  66.         Thread p2 = new Thread(new Runnable() {
  67.  
  68.             public void run() {
  69.                 runPhilosopher(dp, 1);
  70.             }
  71.         });
  72.  
  73.         Thread p3 = new Thread(new Runnable() {
  74.  
  75.             public void run() {
  76.                 runPhilosopher(dp, 2);
  77.             }
  78.         });
  79.  
  80.         Thread p4 = new Thread(new Runnable() {
  81.  
  82.             public void run() {
  83.                 runPhilosopher(dp, 3);
  84.             }
  85.         });
  86.  
  87.         Thread p5 = new Thread(new Runnable() {
  88.  
  89.             public void run() {
  90.                 runPhilosopher(dp, 4);
  91.             }
  92.         });
  93.  
  94.         p1.start();
  95.         p2.start();
  96.         p3.start();
  97.         p4.start();
  98.         p5.start();
  99.  
  100.         p1.join();
  101.         p2.join();
  102.         p3.join();
  103.         p4.join();
  104.         p5.join();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement