import java.util.Random; public class VasesCommuniquants { private static final int QUANTITE_INITIALE = 200; public static final int NB_THREAD_MAX = 3; private static int iteration = 0; private int[] vase = { QUANTITE_INITIALE / 2, QUANTITE_INITIALE / 2 }; public VasesCommuniquants() { for (int i = 0; i < NB_THREAD_MAX; i++) { new ThreadTransfert().start(); } } public static void main(String[] args) { new VasesCommuniquants(); } public int transfert(int qte) { System.out.println("-(" + qte + ") dans la vase 1"); vase[0] -= qte; System.out.println("+(" + qte + ") dans la vase 2"); vase[1] += qte; iteration++; if (iteration % 100 == 0) { System.out.println("" + iteration + "itérations"); } return vase[0] + vase[1]; } public class ThreadTransfert extends Thread { Random r = new Random(); int quantite; public void run() { while (!isInterrupted()) { quantite = 1 + r.nextInt(10); if (transfert(quantite) != QUANTITE_INITIALE) { System.out .println("Quantité totale invalide à l'itération " + iteration); System.exit(-1); } try { Thread.sleep(10); } catch (InterruptedException e) { } } } } }
import java.util.Random; public class VasesCommuniquants { private static final int QUANTITE_INITIALE = 200; public static final int NB_THREAD_MAX = 3; private static int iteration = 0; private int[] vase = { QUANTITE_INITIALE / 2, QUANTITE_INITIALE / 2 }; public VasesCommuniquants() { for (int i = 0; i < NB_THREAD_MAX; i++) { new ThreadTransfert().start(); } } public static void main(String[] args) { new VasesCommuniquants(); } public synchronized int transfert(int qte) { vase[0] -= qte; vase[1] += qte; iteration++; if (iteration % 100 == 0) { System.out.println("" + iteration + "itérations"); } return vase[0] + vase[1]; } public class ThreadTransfert extends Thread { Random r = new Random(); int quantite; public void run() { while (!isInterrupted()) { quantite = 1 + r.nextInt(10); if (transfert(quantite) != QUANTITE_INITIALE) { System.out .println("Quantité totale invalide à l'itération " + iteration); System.exit(-1); } try { Thread.sleep(10); } catch (InterruptedException e) { } } } } }
import java.util.Random; public class VasesCommuniquants { private static final int QUANTITE_INITIALE = 200; public static final int NB_THREAD_MAX = 3; private static int iteration = 0; private int[] vase = { QUANTITE_INITIALE / 2, QUANTITE_INITIALE / 2 }; public VasesCommuniquants() { for (int i = 0; i < NB_THREAD_MAX; i++) { new ThreadTransfert().start(); } } public static void main(String[] args) { new VasesCommuniquants(); } public int transfert(int qte) { synchronized (this) { vase[0] -= qte; vase[1] += qte; iteration++; if (iteration % 100 == 0) { System.out.println(iteration + " itérations"); } } return vase[0] + vase[1]; } public class ThreadTransfert extends Thread { Random r = new Random(); int quantite; public void run() { while (!isInterrupted()) { quantite = 1 + r.nextInt(10); if (transfert(quantite) != QUANTITE_INITIALE) { System.out .println("Quantité totale invalide à l'itération " + iteration); System.exit(-1); } try { Thread.sleep(10); } catch (InterruptedException e) { } } } } }
public class ThreadExtends extends Thread { public void run(){ while (true) { System.out.println("dans ThreadExtends.run"); } } } public class Threads1{ public static void main(String[] args) { ThreadExtends t0= new ThreadExtends (); t0.start(); while (true) { System.out.println("dans thread1.main"); } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question