1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
import java.util.Random;
import java.util.concurrent.Semaphore;
public class client extends Thread
{
Semaphore sema = new Semaphore(1,true); //ICI JE CRÉE MA SÉMAPHORE AVEC 1 EN PARAMÈTRE POUR LE MAX DE RESSOURCE
public client (String Nom)
{
super(Nom);
}
public void run()
{
//this.PrendreCaddie();
this.EntrerMagasin();
this.ChoisirArticle();
this.AllerCaisse();
this.Payer();
this.Sortir();
}
public void PrendreCaddie()
{
}
public void EntrerMagasin()
{
try
{
sema.acquire(); //ICI JE DEMANDE QUE LA SÉMAPHORE DÉCRÉMENTE DE 1 POUR BLOQUER LES CLIENTS (enfin c'est ce que je pense ^^)
}
catch(InterruptedException ie){}
System.out.println(this.getName() + " entre dans le magasin");
}
public void ChoisirArticle()
{
System.out.println(this.getName() + " choisie ses articles.");
Random R = new Random();
int TChoix = (1 + R.nextInt(5 - 0)) * 2000;
try
{
sleep(TChoix);
}
catch(InterruptedException ie){}
System.out.println(this.getName() + " a mis " + TChoix/1000 + "s pour choisir ses articles.");
}
public void AllerCaisse()
{
System.out.println(this.getName() + " se dirige vers les caisses");
}
public void Payer()
{
System.out.println(this.getName() + " paie ses articles.");
Random R = new Random();
int TPayer = (1 + R.nextInt(5 - 0)) * 2000;
try
{
sleep(TPayer);
}
catch(InterruptedException ie){}
System.out.println(this.getName() + " a mis " + TPayer/1000 + "s pour payer ses articles.");
}
public void Sortir()
{
sema.release(); //ICI JE DEMANDE QUE LA SÉMAPHORE INCRÉMENTE DE 1 POUR LIBÉRER un CLIENTS (enfin c'est ce que je pense aussi ^^)
System.out.println(this.getName() + " sort du magasin");
interrupted();
}
} |