-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestigo.java
37 lines (31 loc) · 961 Bytes
/
Testigo.java
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
package ud02.Ejercicios.Ej15_Relevos;
public class Testigo {
private int corredor;
protected int participantes;
public Testigo(int participantes){
this.participantes = participantes;
}
public void setCorredor(int corredor){
this.corredor = corredor;
}
public synchronized void iniciarCarrera(){
System.out.println("Se va a iniciar la carrera.");
setCorredor(1);
notifyAll();
}
public synchronized void tomarTestigo(int corredor){
while (corredor != this.corredor){
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public synchronized void pasarTestigo(int corredor){
if (corredor < participantes) {
System.out.println("Corredor " + corredor + " pasa el testigo a " + ++this.corredor + ".");
}
notifyAll();
}
}