Ecrivez un
programme Java, - modèle d’une piscine: cabines pour se
changer (nombre nc),
paniers pour garder les vêtements (nb) et une piscine à
nager avec nombre de
places sans limite.
Chaque client :
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 = "Clent "+ ++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:Clent 1 creating new client:Clent 2 Clent 1 going to the swim pool Clent 1 try to take basket the basket is taken byClent 1, there is 2 free baskets Clent 1 going to the cabine creating new client:Clent 3 Clent 2 going to the swim pool Clent 1 try to take cabine the cabine is taken by Clent 1, there is 1 free cabines Clent 1 changing Clent 2 try to take basket the basket is taken byClent 2, there is 1 free baskets Clent 2 going to the cabine creating new client:Clent 4 Clent 3 going to the swim pool Clent 2 try to take cabine the cabine is taken by Clent 2, there is 0 free cabines Clent 2 changing Clent 3 try to take basket the basket is taken byClent 3, there is 0 free baskets Clent 3 going to the cabine Clent 3 try to take cabine there is no free cabine, Clent 3 waiting creating new client:Clent 5 Clent 4 going to the swim pool Clent 4 try to take basket there is no free basket,Clent 4 waiting Clent 5 going to the swim pool Clent 5 try to take basket there is no free basket,Clent 5 waiting Clent 1 release cabin the cabine is released by Clent 1, there is 1 free cabines Clent 1 swimimg the cabine is taken by Clent 3, there is 0 free cabines Clent 3 changing Clent 2 release cabin the cabine is released by Clent 2, there is 1 free cabines Clent 2 swimimg Clent 1 try to take cabine the cabine is taken by Clent 1, there is 0 free cabines Clent 1 changing Clent 3 release cabin the cabine is released by Clent 3, there is 1 free cabines Clent 3 swimimg Clent 1 release cabin the cabine is released by Clent 1, there is 2 free cabines Clent 1 release basket the basket is released byClent 1, there is 1 free baskets the basket is taken byClent 5, there is 0 free baskets Clent 5 going to the cabine there is no free basket,Clent 4 waiting Clent 5 try to take cabine the cabine is taken by Clent 5, there is 1 free cabines Clent 5 changing Clent 2 try to take cabine the cabine is taken by Clent 2, there is 0 free cabines Clent 2 changing Clent 5 release cabin the cabine is released by Clent 5, there is 1 free cabines Clent 5 swimimg Clent 2 release cabin the cabine is released by Clent 2, there is 2 free cabines Clent 2 release basket the basket is released byClent 2, there is 1 free baskets the basket is taken byClent 4, there is 0 free baskets Clent 4 going to the cabine Clent 4 try to take cabine the cabine is taken by Clent 4, there is 1 free cabines Clent 4 changing Clent 4 release cabin the cabine is released by Clent 4, there is 2 free cabines Clent 4 swimimg Clent 5 try to take cabine the cabine is taken by Clent 5, there is 1 free cabines Clent 5 changing Clent 3 try to take cabine the cabine is taken by Clent 3, there is 0 free cabines Clent 3 changing Clent 5 release cabin the cabine is released by Clent 5, there is 1 free cabines Clent 5 release basket the basket is released byClent 5, there is 1 free baskets Clent 3 release cabin the cabine is released by Clent 3, there is 2 free cabines Clent 3 release basket the basket is released byClent 3, there is 2 free baskets Clent 4 try to take cabine the cabine is taken by Clent 4, there is 1 free cabines Clent 4 changing Clent 4 release cabin the cabine is released by Clent 4, there is 2 free cabines Clent 4 release basket the basket is released byClent 4, there is 3 free baskets |