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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
public class MegaPhone {
MegaPhone mph;
synchronized void parler( int NumeroDiscours, String qui, Thread t){
System.out.println(qui+"va commencer son discours"+ NumeroDiscours);
for(int i=1 ; i<3 ; i++){
System.out.println(qui+"dit phrase"+ i + "de son discours " +NumeroDiscours);
}
System.out.println(qui +"a terminé son discours "+ NumeroDiscours);
try {
Thread.sleep(2000);
System.out.println("pause...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Orateur extends Thread {
String nom;
MegaPhone mph;
public Orateur(String nom, MegaPhone mph){
this.nom=nom;
this.mph=mph;
}
public void run() {
for(int i=1; i<=2;i++)
{
mph.parler(i, nom, this);
}
}
}
public class Assemblee {
MegaPhone mph;
String nom;
public static void main(String[] args) {
// TODO Auto-generated method stub
MegaPhone mph=new MegaPhone();
Orateur or1 = new Orateur("Orateur 1",mph);
Orateur or2 = new Orateur("Orateur 2",mph);
Orateur or3 = new Orateur("Orateur 3",mph);
// ajout president
Orateur pres=new Orateur("président",mph);
or1.start();
or2.start();
or3.start();
try {
or1.join();
or2.join();
or3.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pres.start();
}
} |
Partager