Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1.  
  2.  
  3. import java.util.HashSet;
  4. import java.util.concurrent.Semaphore;
  5.  
  6. public class BarberShop {
  7.  
  8.  
  9.     static Semaphore clients;
  10.     static Semaphore countLock;
  11.     static Semaphore ready;
  12.     static Semaphore wakeUp;
  13.     static Semaphore leave;
  14.     static int customersNumber;
  15.     static int istrizeniKorisnici;
  16.  
  17.     public static void init() {
  18.         clients = new Semaphore(5);
  19.         countLock = new Semaphore(1);
  20.         wakeUp = new Semaphore(0);
  21.         ready = new Semaphore(0);
  22.         leave = new Semaphore(0);
  23.         customersNumber = 0;
  24.         istrizeniKorisnici = 0;
  25.     }
  26.  
  27.  
  28.     public static class Barber extends Thread {
  29.         @Override
  30.         public void run() {
  31.             try {
  32.                 while (true) {
  33.                     barber();
  34.                 }
  35.             } catch (InterruptedException e) {
  36.                 e.printStackTrace();
  37.             }
  38.         }
  39.     }
  40.  
  41.     public static class Client extends Thread {
  42.         int index = 0;
  43.  
  44.         public Client(int id) {
  45.             index = id;
  46.         }
  47.  
  48.         @Override
  49.         public void run() {
  50.             try {
  51.                 customerComesIn(index);
  52.             } catch (InterruptedException e) {
  53.                 e.printStackTrace();
  54.             }
  55.         }
  56.     }
  57.  
  58.     static int waitingCustomers = 0;
  59.  
  60.     static void customerComesIn(int index) throws InterruptedException {
  61.         clients.acquire();
  62.         countLock.acquire();
  63.         customersNumber++;
  64.         boolean commander = customersNumber % 5 == 0;
  65.         countLock.release();
  66.         if (commander && wakeUp.availablePermits() == 0) {
  67.             wakeUp.release(); // go budi berberceto
  68.         }
  69.         ready.acquire();
  70.         System.out.println(String.format("Klient %d se sisha", index));
  71.         leave.acquire();
  72.         countLock.acquire();
  73.         istrizeniKorisnici++;
  74.         countLock.release();
  75.         clients.release();
  76.  
  77.  
  78.         // TODO: 3/29/20 Synchronize this method, invoked by a Customer thread
  79.     }
  80.  
  81.     static void barber() throws InterruptedException {
  82.  
  83.         System.out.println("Berberot spie...");
  84.         wakeUp.acquire(); //Ceka da go razbudat
  85.         System.out.println("Berberot e razbuden...");
  86.  
  87.         for (int i = 0; i < 5; i++) {
  88.             ready.release();
  89.             leave.release();
  90.         }
  91.  
  92.     }
  93.  
  94.  
  95.     // TODO: 3/29/20 Synchronize this method, invoked by Barber thread
  96.  
  97.  
  98.     public static void main(String[] args) throws InterruptedException {
  99.         init();
  100.         HashSet<Thread> threads = new HashSet<>();
  101.         int NUM_CUSTOMERS = 1200;
  102.         for (int i = 0; i < NUM_CUSTOMERS; i++) {
  103.             threads.add(new Client(i));
  104.  
  105.             // TODO: 3/29/20 Synchronize the scenario
  106.         }
  107.         threads.add(new Barber());
  108.  
  109.         for (Thread t : threads) {
  110.             t.start();
  111.         }
  112.  
  113.         for (Thread t : threads) {
  114.             t.join(1000);
  115.         }
  116.  
  117.         for (Thread t : threads) {
  118.             if (t.isAlive()) {
  119.                 t.interrupt();
  120.             }
  121.         }
  122.         System.out.println(String.format("Vkupno istrizeni glavi: %d", istrizeniKorisnici));
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement