Vous êtes sur la page 1sur 1

public class Compteur2 {

int no=0;
int no_fin;
// Constructeur
Compteur2 (int fin) {
no_fin = fin;
}
// On compte
synchronized void comptons() {
for (int i=0; i<no_fin; i++) {
no++;
}
System.out.println( Thread.currentThread().getName() + "
Total : " + Integer.toString(no));
}}
class LanceCompte2 extends Thread {
Compteur2 cp;
// Constructeur
LanceCompte2(Compteur2 cp) {
this.cp = cp;}
public void run () { // On redéfinit run()
cp.comptons();
}}
class Compter2 {
public static void main (String arg[]) {
Compteur2 cp1 = new Compteur2 (10000);
LanceCompte2 lc1 = new LanceCompte2 (cp1);
LanceCompte2 lc2 = new LanceCompte2 (cp1);
// Démarrage des threads
lc1.start();
lc2.start();

try {
lc1.join();
lc2.join();
} catch (Exception e) { return ;

}
}

}
/*
* Resultat 1
* Thread-0 Total : 10000
Thread-1 Total : 20000

Resultat 2
Thread-0 Total : 10000
Thread-1 Total : 20000
*
* */

Vous aimerez peut-être aussi