Vous êtes sur la page 1sur 4

Correction TP : les Threads

Exercice 1 :
public class Compteur extends Thread{
private String nom;
private int N;
public Compteur(String nom,int max){
super();
this.nom=nom;
this.N=max;
}
public void run(){
for(int i=1; i<this.N; i++){
System.out.println(this.nom+": " +i);
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println (this.nom+" a termin!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Compteur c1=new Compteur("comp1",20);
Compteur c2=new Compteur("comp2", 10);
c1.start();
c2.start();
try {
c1.join();
c2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Tout est termin.");
}
}

En implmentant linterface Runnable :


public class Compteur2 implements Runnable{
private String nom;
private int N;
public Compteur2(String nom,int max){
super();
this.nom=nom;
this.N=max;
}
public void run(){

for(int i=1; i<this.N; i++){


System.out.println(this.nom+": " +i);
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println (this.nom+" a termin!");
}
public static void main(String[] args) {
Compteur2 c1=new Compteur2("comp1",20);
Compteur2 c2=new Compteur2("comp2", 10);
Thread t1=new Thread(c1);
Thread t2=new Thread(c2);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Tout est termin.");
}
}

Exercice 2 :
public class counterAZ implements Runnable{
public void run(){
for(char i='a'; i<='z'; i++)
System.out.println("counterAZ: "+i);
}
}

public class counterNum implements Runnable{


public void run(){
for(int i=1; i<=26; i++)
System.out.println("counter: "+i);
}
}

Programme principal:
public class prog {
public static void main(String[] args) {
counterNum c1=new counterNum();
counterAZ c2=new counterAZ();
Thread t1=new Thread(c1);
Thread t2=new Thread(c2);

t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Correction de Threads.java :
public class Threads extends Thread {
static int j = 1;
public void run() {
for (int i = 1; i<=20; i++) {
System.out.println(j++ + " " + getName()); }
}
public static void main(String[] args) {
Threads t1 = new Threads();
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Threads t2 = new Threads();
t2.start();
}
}

Exercice 3 :
public class compteBC {
private float solde=0;
public float getSolde(){
return this.solde;
}
public void debit(float deb){
System.out.println("dbit de: "+deb );
this.solde=solde-deb;
}
public void credit (float cred){
System.out.println("crdit de: "+cred);
this.solde=solde+cred;
}
}

Correction de OperationsNulles.java:

public class OperationsNulles extends Thread {


private compteBC compte;
private static int nbTh = 10;
private int ID ;
public OperationsNulles(compteBC compte, int i) {
super();
this.compte = compte;
this.ID=i;
}
public void run() {
//while (true) {
int s = (int) (Math.random() * 1000);
synchronized (compte) {
compte.credit(s);
compte.debit(s);
}
System.out.println("solde: "+compte.getSolde()+ " (opration
"+this.ID+")");
//}
}
public static void main(String[] args) {
compteBC compte = new compteBC();
OperationsNulles operations[]=new OperationsNulles[nbTh];
for (int i = 0; i < nbTh; i++) {
operations[i] = new OperationsNulles(compte,i);
operations[i].start();
}
}
}

Vous aimerez peut-être aussi