Advertisement
Jaydeep999997

Unisex Bathroom - S1

Oct 22nd, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 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 Semaphore employeesUsingBathroom = new Semaphore(MAX_CAPACITY);
  62.  
  63.   private String currentGender = NONE;
  64.  
  65.   private void useBathRoom(String name) throws InterruptedException {
  66.     System.out.println(name + " is using the bathroom");
  67.     Thread.sleep(random.nextInt(100));
  68.     System.out.println(name + " is done using the bathroom");
  69.   }
  70.  
  71.   public void maleWantsToGo(String name) throws InterruptedException {
  72.     synchronized (this) {
  73.       while (currentGender.equals(FEMALE)) {
  74.         this.wait();
  75.       }
  76.       currentGender = MALE;
  77.     }
  78.     employeesUsingBathroom.acquire();
  79.  
  80.     useBathRoom(name);
  81.  
  82.     synchronized (this) {
  83.       employeesUsingBathroom.release();
  84.       if (employeesUsingBathroom.availablePermits() == MAX_CAPACITY) {
  85.         currentGender = NONE;
  86.       }
  87.       this.notifyAll();
  88.     }
  89.   }
  90.  
  91.   public void femaleWantsToGo(String name) throws InterruptedException {
  92.     synchronized (this) {
  93.       while (currentGender.equals(MALE)) {
  94.         this.wait();
  95.       }
  96.       currentGender = FEMALE;
  97.     }
  98.     employeesUsingBathroom.acquire();
  99.  
  100.     useBathRoom(name);
  101.  
  102.     synchronized (this) {
  103.       employeesUsingBathroom.release();
  104.       if (employeesUsingBathroom.availablePermits() == MAX_CAPACITY) {
  105.         currentGender = NONE;
  106.       }
  107.     }
  108.   }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement