Advertisement
Jaydeep999997

Unisex Bathroom - Starvation

Oct 22nd, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | Source Code | 0 0
  1. import java.util.Random;
  2. import java.util.concurrent.Semaphore;
  3. import java.util.Set;
  4. import java.util.HashSet;
  5.  
  6. class Demontration {
  7.   public static void main(String[] args) throws Exception {
  8.     UnisexBathroom bathroom = new UnisexBathroom();
  9.     Set<Thread> threads = new HashSet<Thread>();
  10.  
  11.     for (int i = 0; i < 20; i++) {
  12.       final int index = i;
  13.       Thread thread =
  14.           new Thread(
  15.               new Runnable() {
  16.                 @Override
  17.                 public void run() {
  18.                   try {
  19.                     bathroom.maleWantsToGo("Male_" + index);
  20.                   } catch (InterruptedException ie) {
  21.  
  22.                   }
  23.                 }
  24.               });
  25.  
  26.       thread.setName("Male_" + i);
  27.       threads.add(thread);
  28.     }
  29.  
  30.     for (int i = 0; i < 15; i++) {
  31.       final int index = i;
  32.       Thread thread =
  33.           new Thread(
  34.               () -> {
  35.                 try {
  36.                   bathroom.femaleWantsToGo("Female_" + index);
  37.                 } catch (InterruptedException ie) {
  38.  
  39.                 }
  40.               });
  41.       thread.setName("Female_" + i);
  42.       threads.add(thread);
  43.     }
  44.  
  45.     for (Thread thread : threads) {
  46.       thread.start();
  47.     }
  48.  
  49.     for (Thread thread : threads) {
  50.       thread.join();
  51.     }
  52.   }
  53. }
  54.  
  55. class UnisexBathroom {
  56.   private static final String FEMALE = "FEMALE";
  57.   private static final String MALE = "MALE";
  58.   private static final String NONE = "NONE";
  59.   private static final Random random = new Random(System.currentTimeMillis());
  60.   private static final int MAX_CAPACITY = 3;
  61.   private static final int MAX_CONSECUTIVE_USERS = 3;
  62.   private static final Semaphore employeesUsingBathroom = new Semaphore(MAX_CAPACITY);
  63.  
  64.   private String currentGender = NONE;
  65.   private int consecutiveUsers = 0;
  66.  
  67.   private void useBathRoom(String name) throws InterruptedException {
  68.     System.out.println(name + " is using the bathroom");
  69.     Thread.sleep(random.nextInt(100));
  70.     System.out.println(name + " is done using the bathroom");
  71.   }
  72.  
  73.   public void maleWantsToGo(String name) throws InterruptedException {
  74.     synchronized (this) {
  75.       while (currentGender.equals(FEMALE)
  76.           || (currentGender.equals(MALE) && consecutiveUsers >= MAX_CONSECUTIVE_USERS)) {
  77.         this.wait();
  78.       }
  79.       currentGender = MALE;
  80.       consecutiveUsers++;
  81.     }
  82.     employeesUsingBathroom.acquire();
  83.  
  84.     useBathRoom(name);
  85.  
  86.     synchronized (this) {
  87.       employeesUsingBathroom.release();
  88.       if (employeesUsingBathroom.availablePermits() == MAX_CAPACITY) {
  89.         currentGender = NONE;
  90.         consecutiveUsers = 0;
  91.       }
  92.       this.notifyAll();
  93.     }
  94.   }
  95.  
  96.   public void femaleWantsToGo(String name) throws InterruptedException {
  97.     synchronized (this) {
  98.       while (currentGender.equals(MALE)
  99.           || (currentGender.equals(FEMALE) && consecutiveUsers >= MAX_CONSECUTIVE_USERS)) {
  100.         this.wait();
  101.       }
  102.       currentGender = FEMALE;
  103.       consecutiveUsers++;
  104.     }
  105.     employeesUsingBathroom.acquire();
  106.  
  107.     useBathRoom(name);
  108.  
  109.     synchronized (this) {
  110.       employeesUsingBathroom.release();
  111.       if (employeesUsingBathroom.availablePermits() == MAX_CAPACITY) {
  112.         currentGender = NONE;
  113.         consecutiveUsers = 0;
  114.       }
  115.       this.notifyAll();
  116.     }
  117.   }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement