Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package Lab3;
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.Semaphore;
  5.  
  6. public class Exercise5 {
  7.  
  8.     static int waitingCustomers = 0;
  9.  
  10.     static Semaphore availableSeats = new Semaphore(5);
  11.     static Semaphore lock = new Semaphore(1);
  12.     static Semaphore work = new Semaphore(1);
  13.     static Semaphore customerHere = new Semaphore(0);
  14.  
  15.  
  16.     static class Customer extends Thread {
  17.         String ID;
  18.  
  19.         public Customer(String ID) {
  20.             this.ID = ID;
  21.         }
  22.  
  23.         void customerComesIn() throws InterruptedException {
  24.             // TODO: 3/29/20 Synchronize this method, invoked by a Customer thread
  25.             lock.acquire();
  26.             if (waitingCustomers == 5) {
  27.                 System.out.println("No free seats for customer" + ID);
  28.                 lock.release();
  29.             }
  30.             else {
  31.                 System.out.println("Customer " + ID + " coming in...");
  32.                 waitingCustomers++;
  33.                 lock.release();
  34.                 availableSeats.acquire();
  35.                 customerHere.release();
  36.             }
  37.         }
  38.  
  39.         @Override
  40.         public void run() {
  41.             try {
  42.                 customerComesIn();
  43.             } catch (InterruptedException e) {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.     }
  48.  
  49.     static class Barber extends Thread {
  50.         public Barber() {
  51.         }
  52.  
  53.         void barber() throws InterruptedException {
  54.             // TODO: 3/29/20 Synchronize this method, invoked by Barber thread
  55.             while (true) {
  56.                 customerHere.acquire();
  57.                 work.acquire();
  58.                 System.out.println("Haircut...");
  59.                 waitingCustomers--;
  60.                 work.release();
  61.                 availableSeats.release();
  62.             }
  63.         }
  64.  
  65.         @Override
  66.         public void run() {
  67.             try {
  68.                 barber();
  69.             } catch (InterruptedException e) {
  70.                 e.printStackTrace();
  71.             }
  72.         }
  73.     }
  74.  
  75.     public static void main(String[] args) {
  76.         // TODO: 3/29/20 Synchronize the scenario
  77.         Barber barber = new Barber();
  78.         HashSet<Customer> customers = new HashSet<Customer>();
  79.  
  80.         for (int i = 0; i < 150; i++) {
  81.             Customer c = new Customer(Integer.toString(i));
  82.             customers.add(c);
  83.         }
  84.  
  85.         for (Thread t : customers) {
  86.             t.start();
  87.         }
  88.  
  89.         barber.start();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement