Explorer les Livres électroniques
Catégories
Explorer les Livres audio
Catégories
Explorer les Magazines
Catégories
Explorer les Documents
Catégories
Exercice 1 :
Classe Passager :
import java.util.Random;
import java.util.concurrent.Semaphore;
public class Passager extends Thread {
String name;
Semaphore sem;
//Déclarer le sémaphore appelé sem
Random r = new Random();
public Passager(String pName, Semaphore pSem){
name = pName;
sem = pSem;
}
public void run() {
//Simuler la demande d’une autorisation au minibus
try {
sem.acquire();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int pause =r.nextInt(6000)+10000;
System.out.println(name + " : Je reste au minibus pendant " + pause/1000 +
" sec");
try {
sleep(pause);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println(name + " : Au revoir. Je quitte le mini-bus. ");
//Simuler la libération du minibus
sem.release();
}
}
Class Minibus :
import java.util.concurrent.Semaphore;
Interprétation :
Exercice 2 :
Class Sportif :
import java.util.concurrent.Semaphore;
s.RéserverCabine();
sp.start();
}
}
}
Class Salle :
import java.util.concurrent.Semaphore;
Semaphore nombreCabines ;
Semaphore nombreCasiers ;
}
public void RéserverCasier() {
try {
nombreCasiers.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("casier résréver");
}
public void LibérerCasier() {
nombreCasiers.release();
System.err.println("casier libérer");
}
Interprétation :
Exercice 3 :
Class Employe :
package EX3;
int nombre;
Fontaine fontaine ;
int cap;
this.nombre = nombre;
this.fontaine = fontaine;
this.cap = cap;
}
public Employe(int nombre, Fontaine fontaine) {
super();
this.nombre = nombre;
this.fontaine = fontaine;
}
public void run() {
fontaine.remplir_verre(this);
}
Sami Naifar PIITMW G1
}Class Fontaine :
package EX3 ;
import java.util.concurrent.Semaphore;
Employe employe;
int capacitemin;
Semaphore sem;
super();
this.sem = sem;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
capacite=capacite-emp.cap;
}else{
try {
sem.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
Sami Naifar PIITMW G1
System.out.println(capacite);
System.out.println("Remplacement du bouteille...");
try {
Thread.sleep(1000);
capacite=4000;
System.out.println("Bouteille remplacée...");
sem.release();
} catch (InterruptedException e) {
e.printStackTrace();
emp.start();
try {
emp.join();
} catch (InterruptedException e) {
e.printStackTrace();
if (fontaine.capacite<emp.cap) {
new Thread(){
fontaine.remplacer_Bouteille();
Sami Naifar PIITMW G1
};
}.start();
}
}
Interprétation :