Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.Set;
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.locks.ReentrantLock;
- class Demonstration {
- public static void main(String[] args) throws Exception {
- UberRideProblem uberRide = new UberRideProblem();
- Set<Thread> threads = new HashSet<Thread>();
- for (int i = 0; i < 10; i++) {
- Thread thread =
- new Thread(
- new Runnable() {
- @Override
- public void run() {
- try {
- uberRide.requestForDemocrat();
- } catch (InterruptedException e) {
- } catch (BrokenBarrierException e) {
- }
- }
- });
- thread.setName("Democrat_" + i);
- threads.add(thread);
- }
- for (int i = 0; i < 14; i++) {
- Thread thread =
- new Thread(
- new Runnable() {
- @Override
- public void run() {
- try {
- uberRide.requestForRepublican();
- } catch (InterruptedException e) {
- } catch (BrokenBarrierException e) {
- }
- }
- });
- thread.setName("Republican_" + i);
- threads.add(thread);
- }
- for (Thread thread : threads) {
- thread.start();
- }
- for (Thread thread : threads) {
- thread.join();
- }
- }
- }
- class UberRideProblem {
- private int republicanRequests = 0;
- private int democratRequests = 0;
- private Semaphore republicanSemaphore = new Semaphore(0);
- private Semaphore democratSemaphore = new Semaphore(0);
- private ReentrantLock lock = new ReentrantLock();
- private CyclicBarrier barrier = new CyclicBarrier(4);
- private void seated() {
- System.out.println(Thread.currentThread().getName() + " is seated");
- }
- private void drive() {
- System.out.println("Uber rider is started by instructor: " + Thread.currentThread().getName());
- }
- public void requestForRepublican() throws InterruptedException, BrokenBarrierException {
- lock.lock();
- republicanRequests++;
- boolean instructor = false;
- if (republicanRequests >= 4) {
- republicanSemaphore.release(3);
- republicanRequests -= 4;
- instructor = true;
- } else if (republicanRequests >= 2 && democratRequests >= 2) {
- republicanSemaphore.release(1);
- democratSemaphore.release(2);
- republicanRequests -= 2;
- democratRequests -= 2;
- instructor = true;
- } else {
- lock.unlock();
- republicanSemaphore.acquire();
- }
- seated();
- barrier.await();
- if (instructor) {
- drive();
- lock.unlock();
- }
- }
- public void requestForDemocrat() throws InterruptedException, BrokenBarrierException {
- lock.lock();
- democratRequests++;
- boolean instructor = false;
- if (democratRequests >= 4) {
- democratSemaphore.release(3);
- democratRequests -= 4;
- instructor = true;
- } else if (democratRequests >= 2 && republicanRequests >= 2) {
- democratSemaphore.release(1);
- republicanSemaphore.release(2);
- democratRequests -= 2;
- republicanRequests -= 2;
- instructor = true;
- } else {
- lock.unlock();
- democratSemaphore.acquire();
- }
- seated();
- barrier.await();
- if (instructor) {
- drive();
- lock.unlock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement