Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lab3;
- import java.util.concurrent.Semaphore;
- public class Exercise5 {
- int waitingCustomers = 0;
- public Semaphore availableSeats = new Semaphore(5);
- public Semaphore lock = new Semaphore(1);
- public class Customer extends Thread{
- void customerComesIn() throws InterruptedException {
- // TODO: 3/29/20 Synchronize this method, invoked by a Customer thread
- availableSeats.acquire();
- lock.acquire();
- waitingCustomers++;
- lock.release();
- }
- @Override
- public void run() {
- try {
- customerComesIn();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public class Barber extends Thread{
- void barber() throws InterruptedException {
- // TODO: 3/29/20 Synchronize this method, invoked by Barber thread
- lock.acquire();
- if (waitingCustomers > 0) {
- System.out.println("Haircut...");
- waitingCustomers--;
- availableSeats.release();
- }
- lock.release();
- }
- @Override
- public void run() {
- try {
- barber();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- // TODO: 3/29/20 Synchronize the scenario
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement