Advertisement
Josif_tepe

Untitled

Apr 4th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 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[6];
  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.         forks[5] = new Semaphore(1);
  23.  
  24.     }
  25.  
  26.     public void lifecycleOfPhilosopher(int id) throws InterruptedException {
  27.  
  28.         while (true) {
  29.             think();
  30.             eat(id);
  31.         }
  32.     }
  33.  
  34.     void think() throws InterruptedException {
  35.         Thread.sleep(random.nextInt(50));
  36.     }
  37.  
  38.     void eat(int id) throws InterruptedException {
  39.         // TODO: synchronize
  40.         forks[id].acquire();
  41.         forks[(id + 1) % 6].acquire();
  42.         Thread.sleep(50);
  43.         forks[id].release();
  44.         forks[(id + 1) % 6].release();
  45.  
  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.         Thread p6 = new Thread(new Runnable() {
  95.  
  96.             public void run() {
  97.                 runPhilosopher(dp, 5);
  98.             }
  99.         });
  100.  
  101.         p1.start();
  102.         p2.start();
  103.         p3.start();
  104.         p4.start();
  105.         p5.start();
  106.         p6.start();
  107.  
  108.         p1.join();
  109.         p2.join();
  110.         p3.join();
  111.         p4.join();
  112.         p5.join();
  113.         p6.join();
  114.  
  115.     }
  116. }
  117. // 0 1 2 3 4 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement