G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 6th, 2020 220 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Lab3;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.Semaphore;
  6. import java.util.concurrent.locks.Lock;
  7. import java.util.concurrent.locks.ReentrantLock;
  8.  
  9. public class BarberShop {
  10.     static Semaphore customer=new Semaphore(5);
  11.     static Semaphore barber=new Semaphore(1);
  12.     static int totalCustomers=0;
  13.     static int waitingCustomers = 0;
  14.     static Lock lock=new ReentrantLock();
  15.  
  16.     void customerComesIn() throws InterruptedException {
  17.         // TODO: 3/29/20 Synchronize this method, invoked by a Customer thread
  18.         customer.acquire();
  19.         waitingCustomers++;
  20.  
  21.  
  22.     }
  23.  
  24.     void barber() throws InterruptedException {
  25.         // TODO: 3/29/20 Synchronize this method, invoked by Barber thread
  26.         while(totalCustomers>0){
  27.             while(waitingCustomers==0){
  28.                 Thread.sleep(50);
  29.             }
  30.             // Cutting hair....
  31.             System.out.println("Next one please! "+(waitingCustomers--)+" customers left!");
  32.             customer.release();
  33.             lock.lock();
  34.             totalCustomers--;
  35.             lock.unlock();
  36.         }
  37.     }
  38.  
  39.     public static void main(String[] args) throws InterruptedException {
  40.         List<Thread>list=new ArrayList<>();
  41.         Thread barber=new Barber();
  42.         for(int i=0;i<100;i++){
  43.             list.add(new Customer());
  44.         }
  45.         totalCustomers=list.size();
  46.         barber.start();
  47.         for(Thread t:list){
  48.             t.start();
  49.         }
  50.         for(Thread t:list){
  51.             t.join();
  52.         }
  53.         barber.join();
  54.         System.out.println("Finished for today!");
  55.     }
  56.     static class Barber extends Thread{
  57.         BarberShop barber=new BarberShop();
  58.  
  59.         @Override
  60.         public void run() {
  61.             try {
  62.                 barber.barber();
  63.             } catch (InterruptedException e) {
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.     }
  68.     static class Customer extends Thread{
  69.         BarberShop customer=new BarberShop();
  70.  
  71.         @Override
  72.         public void run() {
  73.             try {
  74.                 customer.customerComesIn();
  75.             } catch (InterruptedException e) {
  76.                 e.printStackTrace();
  77.             }
  78.         }
  79.     }
  80. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top