Java:quel output est bonne?

Résolu/Fermé
domxaline - Modifié par domxaline le 13/07/2012 à 17:59
 domxaline - 25 juil. 2012 à 09:04
Bonjour,
j'ai trouvé ce prg dans un livre
en l'essayant avec éclipse, son output n'as pas la même que le livre,bizarre
quelque peut m'aider svp
le prg est suivant:
class A extends Thread
	{
	 public void run ( )
	 {
		 for(int i=1;i<=5;i++)
		 {
			 System.out.println("\t From ThreadA:i="+i);
		 }
		 System.out.println("Exit from A");
	 }
	}
	class B extends Thread
	{
	 public void run ( )
	 {
		 for(int j=1;j<=5;j++)
		 {
			 System.out.println("\t From ThreadB:j="+j);
		 }
		 System.out.println("Exit from B");
	 }
	}
	class C extends Thread
	{
	 public void run ( )
	 {
		 for(int k=1;k<=5;k++)
		 {
			 System.out.println("\t From ThreadC:i="+k);
		 }
		 System.out.println("Exit from C");
	 }
	}
	public class ExThread 
	
	{
		public static void main(String [] args)
		{
			new A().start();
			new B().start();
			new C().start();
		}

	}

output d'éclipse est:
From ThreadA:i=1
From ThreadA:i=2
From ThreadA:i=3
From ThreadA:i=4
From ThreadA:i=5
Exit from A
From ThreadB:j=1
From ThreadB:j=2
From ThreadB:j=3
From ThreadB:j=4
From ThreadB:j=5
Exit from B
From ThreadC:i=1
From ThreadC:i=2
From ThreadC:i=3
From ThreadC:i=4
From ThreadC:i=5
Exit from C

output du livre est:
From ThreadA:i=1
From ThreadA:i=2
From ThreadB:j=1
From ThreadB:j=2
From ThreadC:k=1
From ThreadC:k=2
From ThreadA:i=3
From ThreadA:i=4
From ThreadB:j=3
From ThreadB:j=4
From ThreadC:k=3
From ThreadC:k=4
From ThreadA:i=5
Exit from A
From ThreadB:j=5
Exit from B
From ThreadC:k=5
Exit from C
From ThreadA:i=1
From ThreadA:i=2
From ThreadC:k=1
From ThreadC:k=2
From ThreadA:i=3
From ThreadA:i=4
From ThreadB:j=1
From ThreadB:j=2
From ThreadC:k=3
From ThreadC:k=4
From ThreadA:i=5
Exit from A
From ThreadB:k=4
From ThreadB:j=5
From ThreadC:k=5
Exit from C
From ThreadB:j=5
Exit from B



A voir également:

3 réponses

Terminalex Messages postés 166 Date d'inscription samedi 30 octobre 2010 Statut Membre Dernière intervention 23 juillet 2012 57
14 juil. 2012 à 09:34
le output d'eclipse est bon car celui du livre utilise une boucle sur l'ensemble du programme (on voit que la variable A a plusieurs fois la valeur 1)
est-tu sur d'avoir bien copié le livre?
0
je dois prendre 1er réponse n'est ce pas?
0
Terminalex Messages postés 166 Date d'inscription samedi 30 octobre 2010 Statut Membre Dernière intervention 23 juillet 2012 57
23 juil. 2012 à 09:19
je ne sus pas expert avec java mais oui
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
Modifié par KX le 23/07/2012 à 09:37
Les threads s'exécutent par définition en parallèle sur le même processus, en conséquence il est impossible de prévoir l'ordre d'exécution des instructions, qui pourront a priori être différentes à chaque exécution.

Dans ton exemple, les opérations "start" sont très courtes, donc elles sont terminées avant que le "new" suivant ne se termine (la création de l'objet est une opération plus longue qu'une boucle vide de 1 à 5), donc les threads sont imperceptibles.

Tu peux essayer de d'abord créer tous tes threads (opérations "longues") avant de tous les lancer d'un coup, ce qui te permettra d'observer le parallélisme sur les boucles.

Remarque : dans ton "output du livre", il y a six threads (deux de chaque type), alors qu'il n'y en a que trois (un de chaque) dans ton "output d'eclipse"

public static void main(String [] args)
{
	A a1 = new A(), a2 = new A();
	B b1 = new B(), b2 = new B();
	C c1 = new C(), c2 = new C();

	a1.start();
	b1.start();
	c1.start();
	a2.start();
	b2.start();
	c2.start();
}
La confiance n'exclut pas le contrôle
0
ok merci beaucoup,j'ai compris
0