0% ont trouvé ce document utile (0 vote)
212 vues7 pages

Compte Rendu TP 4

Transféré par

Samy Naifar
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats DOCX, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
212 vues7 pages

Compte Rendu TP 4

Transféré par

Samy Naifar
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats DOCX, PDF, TXT ou lisez en ligne sur Scribd

Sami Naifar PIITMW G1

Compte Rendu TP 4 – Algorithmes Parallè les

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;

public class Minibus {

public static void main(String[] args) {


Sami Naifar PIITMW G1

// Simulez le transport de 20 passagers dans un minibus


comportant 7 sièges

Semaphore semaphore = new Semaphore(7, true);


for(int i=0;i<21;i++){
Thread t1 = new Passager("SR", semaphore);
t1.start();
}

Interprétation :

Exercice 2 :

Class Sportif :

import java.util.concurrent.Semaphore;

public class Sportif extends Thread{


String nom;
Salle s;

public Sportif(String nom, Salle s) {


this.nom = nom;
this.s = s;
}

public void ChangerTenu() {

s.RéserverCabine();

System.out.println(nom+": Tenu changer");


try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
s.LibérerCabine();
}

public void Jouer() {


System.out.println(nom+": Joueur est entrain de jouer");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(nom+": fin de session");
Sami Naifar PIITMW G1

public void run() {


ChangerTenu();
s.RéserverCasier();
Jouer();
s.LibérerCasier();
ChangerTenu();

public static void main(String[] args) {

Semaphore nombreCabines = new Semaphore(2);

Semaphore nombreCasiers = new Semaphore(10);

Salle salle = new Salle(nombreCabines, nombreCasiers);

for (int i = 0; i < 20; i++) {

Sportif sp = new Sportif(""+i, salle);

sp.start();
}
}
}

Class Salle :

import java.util.concurrent.Semaphore;

public class Salle {

Semaphore nombreCabines ;

Semaphore nombreCasiers ;

public Salle(Semaphore nombreCabines, Semaphore nombreCasiers) {


this.nombreCabines = nombreCabines;
this.nombreCasiers = nombreCasiers;
}

public void RéserverCabine() {


try {
nombreCabines.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Cabine résréver");
}

public void LibérerCabine() {


nombreCabines.release();
System.err.println("Cabine libérer");
Sami Naifar PIITMW G1

}
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;

public class Employe extends Thread{

int nombre;

Fontaine fontaine ;

int cap;

public Employe(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;

public class Fontaine {

int capacite= 4000;

Employe employe;

int capacitemin;

Semaphore sem;

public Fontaine(Semaphore sem) {

super();

this.sem = sem;
}

public void remplir_verre(Employe emp){


capacitemin=emp.cap;
System.out.println("Employe "+emp.nombre+" place son verre de
"+emp.cap+" ml");
if (capacite>emp.cap){

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();
}

capacite=capacite-emp.cap;

System.out.println("Le verre est rempli... ");

}else{

try {

System.out.println("Bouteille est vide en attente de


son remplacement");
//wait();

sem.acquire();

} catch (InterruptedException e) {

e.printStackTrace();

}
Sami Naifar PIITMW G1

System.out.println(capacite);

public void remplacer_Bouteille(){

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();

public static void main(String[] args) {

Semaphore sem = new Semaphore(1);

final Fontaine fontaine = new Fontaine(sem);

for (int i = 1; i <= 25; i++) {

int cap = (int) (Math.random() * ((500 - 100) + 1)) + 100;

Employe emp = new Employe(i, fontaine,cap);

emp.start();

try {

emp.join();

} catch (InterruptedException e) {

e.printStackTrace();

if (fontaine.capacite<emp.cap) {

new Thread(){

public void run() {

fontaine.remplacer_Bouteille();
Sami Naifar PIITMW G1

};

}.start();

}
}

Interprétation :

Vous aimerez peut-être aussi