Advertisement
Guest User

/./././/././././././././/.

a guest
Apr 7th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.concurrent.Semaphore;
  4.  
  5. public class BarberShop {
  6.  
  7.     static  int waitingCustomers = 0;
  8.  
  9.     static Semaphore berber = new Semaphore(0);
  10.     static Semaphore client = new Semaphore(5);
  11.     static Semaphore lock = new Semaphore(1);
  12.  
  13.     static class Barber extends Thread{
  14.  
  15.         BarberShop barberShop;
  16.  
  17.         public Barber (BarberShop barberShop){
  18.             this.barberShop= barberShop;
  19.         }
  20.         public void execute() throws InterruptedException {
  21.  
  22.             berber.acquire();
  23.             lock.acquire();
  24.             waitingCustomers--;
  25.             barberShop.barber();
  26.             if (waitingCustomers == 0){
  27.                 client.release(5);
  28.             }
  29.             lock.release();
  30.  
  31.         }
  32.         @Override
  33.         public void run() {
  34.  
  35.             for (int i=0;i<100; i++)
  36.                 try {
  37.                     execute();
  38.                 } catch (InterruptedException e) {
  39.                     e.printStackTrace();
  40.                 }
  41.         }
  42.     }
  43.  
  44.     static class Costumer extends Thread{
  45.  
  46.         BarberShop barberShop;
  47.  
  48.         public Costumer (BarberShop barberShop){
  49.             this.barberShop= barberShop;
  50.         }
  51.         public void execute() throws InterruptedException {
  52.  
  53.             client.acquire();
  54.             lock.acquire();
  55.             waitingCustomers++;
  56.             barberShop.customerComesIn();
  57.  
  58.             if(waitingCustomers==5){
  59.                 berber.release(5);
  60.             }
  61.  
  62.             lock.release();
  63.  
  64.  
  65.  
  66.         }
  67.         @Override
  68.  
  69.         public void run() {
  70.  
  71.             for (int i = 0; i < 1; i++) {
  72.  
  73.                 try {
  74.                     execute();
  75.                 } catch (InterruptedException e) {
  76.                     e.printStackTrace();
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     void customerComesIn() throws InterruptedException {
  83.         // TODO: 3/29/20 Synchronize this method, invoked by a Customer thread
  84.         System.out.println("Costumer comes in");
  85.     }
  86.  
  87.     void barber() throws InterruptedException {
  88.         // TODO: 3/29/20 Synchronize this method, invoked by Barber thread
  89.         System.out.println("Barber working");
  90.  
  91.     }
  92.  
  93.     public static void main(String[] args) throws InterruptedException {
  94.         // TODO: 3/29/20 Synchronize the scenario
  95.  
  96.         BarberShop barberShop = new BarberShop();
  97.         Barber b = new Barber(barberShop);
  98.         List<Costumer> costumers =new ArrayList<>();
  99.         for (int i=0 ; i<100 ; i++){
  100.             costumers.add(new Costumer(barberShop));
  101.         }
  102.  
  103.         b.start();
  104.         for (Costumer c :costumers){
  105.             c.start();
  106.         }
  107.         b.join();
  108.         for (Costumer c :costumers){
  109.             c.join(5);
  110.         }
  111.  
  112.         System.out.println("Uspeshna sinhronizacija");
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement