-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExLibro.java
55 lines (42 loc) · 1.06 KB
/
ExLibro.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
/*pagina 145 libro di p3*/
public class Ex{
Stato s=new Stato();
int nT;
Ex(int k){this.nT=k;}
public class Stato{
int st=0;
synchronized void changeStato(){
st=((st+1)%nT);
this.notifyAll();
}
}
public class T extends Thread{
int n;
T(String i){
super(i);
n=(Ex.this.nT++);
}
public void run(){
while(true){
synchronized (Ex.this.s){
try{
while(Ex.this.s.st!=n){
Ex.this.s.wait();
}
s.changeStato();
}catch(InterruptedException e){}
System.out.println(this);
}
}
}
}
public static void main(String []a){
Ex e=new Ex(0);
Ex.T t1=e.new T("essere");
Ex.T t2=e.new T("o");
Ex.T t3=e.new T("essere carlo");
t1.start();
t2.start();
t3.start();
}
}