//La classe exemple implémente l'interface Runnable public void Exemple implements Runnable { //Thread qui fait tourner l'exemple private Thread thread; public Exemple() { //On accroche le thread à la classe thread=new Thread(this); } //Lance le thread public void lancer() { thread.start(); } //code effectué par le thread //Le nom de run est obligatoire, c'est cette méthode qui est appelée par le thread public void run() { //heu.. ecrit 100 fois exemple et son numéro for(int i=0;i<100;i++) { System.out.println("exemple : "+i); //Attends 100 ms try{thread.sleep(100);}catch(InterruptedException ie){} } //Fin du thread } }
Thread thread=new Thread() { public void run() { //heu.. ecrit 100 fois exemple et son numéro for(int i=0;i<100;i++) { System.out.println("exemple : "+i); //Attends 100 ms try{sleep(100);}catch(InterruptedException ie){} } //Fin du thread } }; thread.start();