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
| class Jeu{
int tour=1;
public synchronized void jouer1() {
if(tour==2){
try{ wait();}
catch(InterruptedException e){}
}
System.out.print("A");
tour=2;
notify();
}
public synchronized void jouer2() {
if(tour==1){
try{ wait();}
catch(InterruptedException e2 ){}
}
if (tour==2){System.out.print("B");
tour=1;
notify();
}
}
}
public class Joueur extends Thread {
char x;
Joueur(char x){this.x=x;}
public static void main(String[] args){
Joueur j1= new Joueur('A');
j1.start();
Joueur j2= new Joueur('B');
j2.start();
}
public void run(){while(true){
if(x=='A'){
new Jeu().jouer1();}
if(x=='B'){
new Jeu().jouer2(); }
}
}
} |
Partager