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
|
public class MaThread extends Thread {
/** Un attribut propre à chaque thread */
private String threadName;
/** Création et démarrage automatique du thread */
public MaThread(String threadName) {
this.threadName = threadName;
this.start();
}
/** Le but d'un tel thread est d'afficher indéfiniment
* son attribut threadName toutes les 10secondes . Notons que la méthode
* sleep peut déclencher des exceptions.
*/
public void run() {
try {
while(true) {
System.out.println("Thread nommé : " + this.threadName + " - itération : " + i);
Thread.sleep(10000);
}
} catch (InterruptedException exc) {exc.printStackTrace();}
}
static public void main(String argv[]) {
MaThread thr1 = new MaThread("Toto");
MaThread thr2 = new MaThread("Test2");
}
} |