-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPassaggio.java
58 lines (45 loc) · 1.26 KB
/
Passaggio.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
import java.util.Vector;
/**
* Created by aless on 23/06/2017.
*/
class Passaggio{ }
class Stop extends Thread{
Passaggio p;
Vector<String> o= new Vector<String>();
Stop(Passaggio x, String a){p=x; this.aggiungi(a);}
public void run(){
try {
while(true){
synchronized (this) {
while (o.size() == 0) {
this.wait();
}
}
synchronized (p){
for (int i = 0; i <= o.size(); i++) {
System.out.println(o.get(i) + "macchina in transito");
o.remove(i);
}
}
}
}catch(InterruptedException e){
}
}
public synchronized void aggiungi(String a){
o.add(a);
this.notifyAll();
}
}
public class Avvio{
public static void main(String[] arg) {
Passaggio p =new Passaggio();
Stop s1=new Stop(p,new String("a"));
Stop s2=new Stop(p,new String("b"));
s1.start();
s2.start();
for(int i=0; i<30; i++){
s1.aggiungi("e");
s2.aggiungi("pippo");
}
}
}