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
| public class CompteurRunnable implements Runnable {
private String nom;
private int max;
public CompteurRunnable(String nom, int max) {
this.nom = nom;
this.max = max;
}
public CompteurRunnable(String nom) {
this(nom, 10);
}
public String getNom() {
return nom;
}
public int getMax() {
return max;
}
public void run() {
for (int i = 1; i <= max; i++) {
try {
Thread.sleep((int)(Math.random() * 5000));
}
catch(InterruptedException e) {
System.err.println(getNom() + " a ete interrompu.");
}
System.out.println(getNom() + " : " + i);
}
System.out.println("*** " + getNom() + " a termine.");
}
public static void main(String[] args) {
CompteurRunnable[] compteurs = {
new CompteurRunnable("Toto"),
new CompteurRunnable("Bibi"),
new CompteurRunnable("Robert"),
new CompteurRunnable("Pierre")
};
for (int i = 0; i < compteurs.length; i++) {
new Thread(compteurs[i]).start();
}
}
} |