Advertisement
Martina312

Untitled

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