-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAgenzia.java
More file actions
69 lines (62 loc) · 1.41 KB
/
Copy pathAgenzia.java
File metadata and controls
69 lines (62 loc) · 1.41 KB
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
class Persona extends Thread{
String nome;
Viaggio v;
Persona(String o, Viaggio vi){this.nome; this.v=vi;}
public void run(){
if(v.prenota()){
System.out.println("confermato viaggio per "+nome);
}
else{
synchronized(v){
try{
while(!chiuso && !confermato){
viaggio.wait();
}
}catch(InterruptedException e){}
}
}
}
}
class Viaggio{
int nMinimo;
String nome;
boolean confermato =false;
boolean chiuso=false;
Viaggio(String n, int o){this.nome=n; this.nMinimo=o;}
Vector<Persona> codaViaggiatori;
boolean synchronized prenota(Persona p){
codaViaggiatori.add(p);
if(codaViaggiatori.size()<nMinimo){
return false;
}
confermato=true;
this.notifyAll();
return true;
}
void synchronized chiudi(){
chiuso=true;
if(confermato){
for(int i=0; i<codaViaggiatori.size(); i++)
System.out.println(codaViaggiatori.get(i));
}
else{
System.out.println("Viaggio non confermato ma CHIUSO");
this.notifyAll();
}
}
}
public class Agenzia {
public static void main(String[] a){
Viaggio v=new Viaggio("Namibia",4);
Persona p1=new Persona("Alice",v);
Persona p2=new Persona("Bob",v);
Persona p3=new Persona("Carl",v);
Persona p4=new Persona("Doug",v);
Persona p5=new Persona("Eric",v);
Persona p6=new Persona("Frank",v);
p1.start();p2.start();p3.start();
p4.start();p5.start();p6.start();
try{ Thread.sleep(200); }catch(Exception e){}
v.chiudi();
}
}