public class Compteur extends Thread { @Override public void run(){ for(int i=0; i<50; i++){ System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ new Compteur().start(); } }
public class Compteur implements Runnable { @Override public void run(){ for(int i=0; i<50; i++){ System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ new Thread(new Compteur()).start(); } }
public static void main(String[] args){ new Thread(new Compteur()).start(); new Thread(new Compteur()).start(); }
0
0
1
1
etc.
Méthode | Description |
---|---|
start() | Exécute le Thread |
join() | Permet d'attendre que le thread soit terminé pour continuer |
join(long delaiGarde) | Permet d'attendre que le thread courant soit terminé ou que le délai de garde soit dépassé pour continuer |
wait() | Permet d'attendre que le thread courant appelle la méthode notify() ou notifyAll() pour continuer |
notify() | Indique au Thread courant qu'il peut continuer son exécution s'il a été interrompu par la méthode wait() |
public class Compteur implements Runnable { @Override public void run(){ for(int i=0; i<3; i++){ System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ Thread t1=new Thread(new Compteur()); Thread t2=new Thread(new Compteur()); t1.start(); try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } t2.start(); } }
Avis de décès, Carte de voeux, Bricolage, Coloriages, Cinéma, Coiffure, Cuisine, Déco, Dictionnaire, Horoscope, Jeux en ligne, Programme TV, Recettes, Restaurant, SMIC, Test débit, Voyage, Signification prénom