-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTorre.java
85 lines (71 loc) · 2.38 KB
/
Torre.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
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
import java.lang.Math; // headers MUST be above the first class
public class Torre{
Integer nInEntrata = 0;
Integer nInAlto = 0;
Object scale=new Object();
Object lock_entrata=new Object();
class GuardiaEntrata extends Thread{
public void run(){
while(true){
int k=0;
synchronized(lock_entrata){
try{
while(Torre.this.nInEntrata==0){
System.out.println("sto aspettando persone da uccidere");
lock_entrata.wait();
}
if(Torre.this.nInEntrata<10){
k=Torre.this.nInEntrata;
}
else{k=10;}
nInEntrata-=k;
System.out.println("messe in coda "+k+" persone");
}catch(InterruptedException e){}
}
synchronized(scale){
Torre.this.nInAlto+=k;
System.out.println("saliti in " +k);
scale.notify();
}
}
}
}
class GuardiaCima extends Thread{
public void run(){
while(true){
synchronized(scale){
try{
while(nInAlto==0){
scale.wait();
}
if(Torre.this.nInAlto<10){
Torre.this.nInAlto=0;
}
else{Torre.this.nInAlto-=10;}
System.out.println("usciti in 10");
scale.notify();
}catch(InterruptedException e){}
}
}
}
}
public void entrano(int k){
synchronized (lock_entrata){
nInEntrata+=k;
System.out.println("arrivati in "+k);
lock_entrata.notifyAll();
}
}
public static void main(String []a){
Torre carlo=new Torre();
GuardiaCima gc=carlo.new GuardiaCima();
GuardiaEntrata ge=carlo.new GuardiaEntrata();
ge.start();
gc.start();
for(int i=0;i<50; i++){
int k=(int)(1+ Math.random()*5);
carlo.entrano(k);
System.out.println("ARRIVATI "+k);
}
}
}