prometheus800

ОС Лаб 3: "Dining Philosophers"

Mar 31st, 2021
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Random;
  3. import java.util.concurrent.Semaphore;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. class Demo {
  8.     public static final int NUM_PHILOSOPHERS = 100;
  9.  
  10.     public static void main(String[] args) throws InterruptedException {
  11.         long startTime = System.nanoTime();
  12.         DiningPhilosophers.runTest();
  13.         long endTime = System.nanoTime();
  14.  
  15.         System.out.printf("It took %f seconds for %d philosophers to eat and think %d times each.",
  16.                 (endTime - startTime) * 0.000000001, NUM_PHILOSOPHERS, NUM_PHILOSOPHERS);
  17.     }
  18. }
  19.  
  20. public class DiningPhilosophers {
  21.  
  22.     static final int NUM_PHILOSOPHERS = Demo.NUM_PHILOSOPHERS;
  23.     private static final Random random = new Random(System.currentTimeMillis());
  24.     private final Semaphore[] forks = new Semaphore[NUM_PHILOSOPHERS];
  25.     static Lock lock;
  26.  
  27.  
  28.     public DiningPhilosophers() {
  29.         for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
  30.             forks[i] = new Semaphore(1);
  31.         }
  32.         lock = new ReentrantLock();
  33.     }
  34.  
  35.     public void lifecycleOfPhilosopher(int id) throws InterruptedException {
  36.         for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
  37.             think();
  38.             eat(id);
  39.         }
  40.     }
  41.  
  42.     void think() throws InterruptedException {
  43.         Thread.sleep(random.nextInt(50));
  44.     }
  45.  
  46.     void eat(int id) {
  47.         // TODO: synchronize
  48.         // Each philosopher will try and grab the forks on his left and right side
  49.         // E.g we have 100 philosophers and 100 forks between them
  50.         // Philosopher with id 99(the last one) will try and grab the 99th and the 1st(fork[0]) fork
  51.         lock.lock();
  52.         if (forks[id % NUM_PHILOSOPHERS].tryAcquire() &&
  53.                 forks[(id + 1) % NUM_PHILOSOPHERS].tryAcquire()) {
  54.             System.out.printf("Philosopher %d eating. . .\n", id);
  55.             forks[id % NUM_PHILOSOPHERS].release();
  56.             forks[(id + 1) % NUM_PHILOSOPHERS].release();
  57.         }
  58.         lock.unlock();
  59.     }
  60.  
  61.     static void runPhilosopher(DiningPhilosophers dp, int id) {
  62.         try {
  63.             dp.lifecycleOfPhilosopher(id);
  64.         } catch (InterruptedException ie) {
  65.             ie.printStackTrace();
  66.         }
  67.     }
  68.  
  69.     public static void runTest() throws InterruptedException {
  70.         final DiningPhilosophers dp = new DiningPhilosophers();
  71.  
  72.         HashSet<Thread> threads = new HashSet<>();
  73.  
  74.         for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
  75.             int finalI = i;
  76.             threads.add(new Thread(() -> runPhilosopher(dp, finalI)));
  77.         }
  78.  
  79.         for (Thread t : threads)
  80.             t.start();
  81.  
  82.         for (Thread t : threads)
  83.             t.join();
  84.     }
  85. }
  86.  
Add Comment
Please, Sign In to add comment