Advertisement
Jaydeep999997

Uber Ride

Oct 22nd, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | Source Code | 0 0
  1. import java.util.HashSet;
  2. import java.util.Set;
  3. import java.util.concurrent.BrokenBarrierException;
  4. import java.util.concurrent.CyclicBarrier;
  5. import java.util.concurrent.Semaphore;
  6. import java.util.concurrent.locks.ReentrantLock;
  7.  
  8. class Demonstration {
  9.   public static void main(String[] args) throws Exception {
  10.     UberRideProblem uberRide = new UberRideProblem();
  11.     Set<Thread> threads = new HashSet<Thread>();
  12.  
  13.     for (int i = 0; i < 10; i++) {
  14.       Thread thread =
  15.           new Thread(
  16.               new Runnable() {
  17.  
  18.                 @Override
  19.                 public void run() {
  20.                   try {
  21.                     uberRide.requestForDemocrat();
  22.                   } catch (InterruptedException e) {
  23.  
  24.                   } catch (BrokenBarrierException e) {
  25.  
  26.                   }
  27.                 }
  28.               });
  29.  
  30.       thread.setName("Democrat_" + i);
  31.       threads.add(thread);
  32.     }
  33.  
  34.     for (int i = 0; i < 14; i++) {
  35.       Thread thread =
  36.           new Thread(
  37.               new Runnable() {
  38.  
  39.                 @Override
  40.                 public void run() {
  41.                   try {
  42.                     uberRide.requestForRepublican();
  43.                   } catch (InterruptedException e) {
  44.  
  45.                   } catch (BrokenBarrierException e) {
  46.  
  47.                   }
  48.                 }
  49.               });
  50.  
  51.       thread.setName("Republican_" + i);
  52.       threads.add(thread);
  53.     }
  54.  
  55.     for (Thread thread : threads) {
  56.       thread.start();
  57.     }
  58.  
  59.     for (Thread thread : threads) {
  60.       thread.join();
  61.     }
  62.   }
  63. }
  64.  
  65. class UberRideProblem {
  66.   private int republicanRequests = 0;
  67.   private int democratRequests = 0;
  68.   private Semaphore republicanSemaphore = new Semaphore(0);
  69.   private Semaphore democratSemaphore = new Semaphore(0);
  70.   private ReentrantLock lock = new ReentrantLock();
  71.   private CyclicBarrier barrier = new CyclicBarrier(4);
  72.  
  73.   private void seated() {
  74.     System.out.println(Thread.currentThread().getName() + " is seated");
  75.   }
  76.  
  77.   private void drive() {
  78.     System.out.println("Uber rider is started by instructor: " + Thread.currentThread().getName());
  79.   }
  80.  
  81.   public void requestForRepublican() throws InterruptedException, BrokenBarrierException {
  82.     lock.lock();
  83.     republicanRequests++;
  84.     boolean instructor = false;
  85.  
  86.     if (republicanRequests >= 4) {
  87.       republicanSemaphore.release(3);
  88.       republicanRequests -= 4;
  89.       instructor = true;
  90.     } else if (republicanRequests >= 2 && democratRequests >= 2) {
  91.       republicanSemaphore.release(1);
  92.       democratSemaphore.release(2);
  93.       republicanRequests -= 2;
  94.       democratRequests -= 2;
  95.       instructor = true;
  96.     } else {
  97.       lock.unlock();
  98.       republicanSemaphore.acquire();
  99.     }
  100.  
  101.     seated();
  102.     barrier.await();
  103.  
  104.     if (instructor) {
  105.       drive();
  106.       lock.unlock();
  107.     }
  108.   }
  109.  
  110.   public void requestForDemocrat() throws InterruptedException, BrokenBarrierException {
  111.     lock.lock();
  112.     democratRequests++;
  113.     boolean instructor = false;
  114.  
  115.     if (democratRequests >= 4) {
  116.       democratSemaphore.release(3);
  117.       democratRequests -= 4;
  118.       instructor = true;
  119.     } else if (democratRequests >= 2 && republicanRequests >= 2) {
  120.       democratSemaphore.release(1);
  121.       republicanSemaphore.release(2);
  122.       democratRequests -= 2;
  123.       republicanRequests -= 2;
  124.       instructor = true;
  125.     } else {
  126.       lock.unlock();
  127.       democratSemaphore.acquire();
  128.     }
  129.  
  130.     seated();
  131.     barrier.await();
  132.  
  133.     if (instructor) {
  134.       drive();
  135.       lock.unlock();
  136.     }
  137.   }
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement