Плувен басейн

   Напишете програма на Java, която моделира ползването на обществен плувен басейн: Басейнът предоставя кабини за преобличане (на брой nc), кошове за съхранение на дрехите на гардероб (на брой nb) и водна площ за плуване (с неограничен брой места). През определен случаен интервал от време се появяват клиенти,които искат ползват басейна.

Всеки клиент:
1) пристига до басейна;
2) взема кабина за преобличане и кош за дрехите;
3) преоблича се;
4) освобождава кабината и задържа коша на гардероб;
5) плува;
6) взима кабина за преобличане като носи коша със себе си;
7) преоблича се;
8) oсвобождава кабината и коша;
9) излиза от басейна.

Въведете организация, която не позволява взаимно блокиране между ползващите трите ресурса клиенти.
Кабина
public class Cabine {
    private int free;
    Cabine(int free){
        this.free = free;
    }
    synchronized void takeCabine(){
        while(free==0){
            System.out.println("there is no free cabine, "+Thread.currentThread().getName()+" waiting");
            try{     wait();   }
            catch(InterruptedException e){
                System.err.println(e);
            }
        }
        free--;
        System.out.println("the cabine is taken by "+Thread.currentThread().getName()+", there is "+free+" free cabines");
    }
    synchronized void releaseCabine(){
        free++;
        System.out.println("the cabine is released by "+Thread.currentThread().getName()+", there is "+free+" free cabines");
        notifyAll();
    }
}

Кош за дрехи
public class Basket {
    private  int free;
    Basket(int free){
        this.free = free;
    }
    synchronized void takeBasket(){
        while(free==0){
            System.out.println("there is no free basket,"+Thread.currentThread().getName()+" waiting");
            try{     wait();   }
            catch(InterruptedException e){
                System.err.println(e);
            }
        }
        free--;
        System.out.println("the basket is taken by"+Thread.currentThread().getName()+", there is "+free+" free baskets");
    }
    synchronized void releaseBasket(){
        free++;
        System.out.println("the basket is released by"+Thread.currentThread().getName()+", there is "+free+" free baskets");
        notifyAll();
    }
}


Клиент
public class Client extends Thread{
    String  name;
    static int n=0;
    Cabine c;
    Basket b;
    Client(Cabine c, Basket b){
        name = "Client "+ ++n;
        this.c=c;
        this.b = b;
        try {
            System.out.println(" creating new client:"+name);
            sleep((int)(Math.random()*50));
        } catch (InterruptedException e){}
        super.setName(name);
    }
    public void run(){
        try {
            System.out.println(this+" going to the swim pool");
            sleep((int)(Math.random()*50));
        } catch (InterruptedException e){}
        System.out.println(this+" try to take basket");
        b.takeBasket();
        try {
            System.out.println(this+" going to the cabine");
            sleep((int)(Math.random()*50));
        } catch (InterruptedException e){}
        System.out.println(this+" try to take cabine");
        c.takeCabine();
        try {
            System.out.println(this+" changing");
            sleep((int)(Math.random()*600));
        } catch (InterruptedException e){}
        System.out.println(this+" release cabin");
        c.releaseCabine();
        try {         
            System.out.println(this+" swimimg");
            sleep((int)(Math.random()*2000));
        } catch (InterruptedException e){}
        System.out.println(this+" try to take cabine");
        c.takeCabine();
        try {         
            System.out.println(this+" changing");
            sleep((int)(Math.random()*600));
        } catch (InterruptedException e){}
        System.out.println(this+" release cabin");
        c.releaseCabine();
        System.out.println(this+" release basket");
        b.releaseBasket();
    }
    public String toString(){
        return name;
    }
}

Апликация плувен басейн
public class SwimPool {
    public static void main(String[] args) {
        Cabine c=new Cabine(2);
        Basket b = new Basket(3);
        for(int i = 0; i<5;i++){
            (new Client(c,b)).start();
        }
    }
}
  creating new client:Client 1
 creating new client:Client 2
Client 1 going to the swim pool
Client 1 try to take basket
the basket is taken byClient 1, there is 2 free baskets
Client 1 going to the cabine
 creating new client:Client 3
Client 2 going to the swim pool
Client 1 try to take cabine
the cabine is taken by Client 1, there is 1 free cabines
Client 1 changing
Client 2 try to take basket
the basket is taken byClient 2, there is 1 free baskets
Client 2 going to the cabine
 creating new client:Client 4
Client 3 going to the swim pool
Client 2 try to take cabine
the cabine is taken by Client 2, there is 0 free cabines
Client 2 changing
Client 3 try to take basket
the basket is taken byClient 3, there is 0 free baskets
Client 3 going to the cabine
Client 3 try to take cabine
there is no free cabine, Client 3 waiting
 creating new client:Client 5
Client 4 going to the swim pool
Client 4 try to take basket
there is no free basket,Client 4 waiting
Client 5 going to the swim pool
Client 5 try to take basket
there is no free basket,Client 5 waiting
Client 1 release cabin
the cabine is released by Client 1, there is 1 free cabines
Client 1 swimimg
the cabine is taken by Client 3, there is 0 free cabines
Client 3 changing
Client 2 release cabin
the cabine is released by Client 2, there is 1 free cabines
Client 2 swimimg
Client 1 try to take cabine
the cabine is taken by Client 1, there is 0 free cabines
Client 1 changing
Client 3 release cabin
the cabine is released by Client 3, there is 1 free cabines
Client 3 swimimg
Client 1 release cabin
the cabine is released by Client 1, there is 2 free cabines
Client 1 release basket
the basket is released byClient 1, there is 1 free baskets
the basket is taken byClient 5, there is 0 free baskets
Client 5 going to the cabine
there is no free basket,Client 4 waiting
Client 5 try to take cabine
the cabine is taken by Client 5, there is 1 free cabines
Client 5 changing
Client 2 try to take cabine
the cabine is taken by Client 2, there is 0 free cabines
Client 2 changing
Client 5 release cabin
the cabine is released by Client 5, there is 1 free cabines
Client 5 swimimg
Client 2 release cabin
the cabine is released by Client 2, there is 2 free cabines
Client 2 release basket
the basket is released byClient 2, there is 1 free baskets
the basket is taken byClient 4, there is 0 free baskets
Client 4 going to the cabine
Client 4 try to take cabine
the cabine is taken by Client 4, there is 1 free cabines
Client 4 changing
Client 4 release cabin
the cabine is released by Client 4, there is 2 free cabines
Client 4 swimimg
Client 5 try to take cabine
the cabine is taken by Client 5, there is 1 free cabines
Client 5 changing
Client 3 try to take cabine
the cabine is taken by Client 3, there is 0 free cabines
Client 3 changing
Client 5 release cabin
the cabine is released by Client 5, there is 1 free cabines
Client 5 release basket
the basket is released byClient 5, there is 1 free baskets
Client 3 release cabin
the cabine is released by Client 3, there is 2 free cabines
Client 3 release basket
the basket is released byClient 3, there is 2 free baskets
Client 4 try to take cabine
the cabine is taken by Client 4, there is 1 free cabines
Client 4 changing
Client 4 release cabin
the cabine is released by Client 4, there is 2 free cabines
Client 4 release basket
the basket is released byClient 4, there is 3 free baskets