-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10.014.java
More file actions
154 lines (129 loc) · 2.33 KB
/
10.014.java
File metadata and controls
154 lines (129 loc) · 2.33 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*B*/
interface D extends Remote{ //di tutti
public void trylock()throws RemoteException;
}
public class DImpl extends UnicastRemoteObject implements D{//host1
Object lock= new Object();
static int cond=0;
DImpl()throws RemoteException{};
public void trylock()throws RemoteException{
synchronized(lock){
try{
cond++;
while(cond<2){
lock.wait();
}
if(cond==2){
cond++;
lock.notify();
}
}catch(InterruptedException e){}
}
}
}
class C1{//host1
void printC1(String s){
System.out.println(s);
}
}
class C2{//host2
void printC2(String s){
System.out.println(s);
}
}
public class HOST1{
public static void main(String []a){
D d=new DImpl();
Naming.rebind("d", d);
C1 c=new C1();
new Thread(){
public void run(){
for(int i=0; i<5; i++){
c.printC1("A");
}
d.trylock();
for(int i=0; i<5; i++){
c.printC1("B");
}
}
}.start();
}
}
public class HOST2{
public static void main(String []a){
D e= (D)Naming.lookup("d");
C2 d=new C2();
new Thread(){
public void run(){
for(int i=0; i<5; i++){
d.printC2("C");
}
e.trylock();
for(int i=0; i<5; i++){
d.printC2("D");
}
}
}.start();
}
}
/*A*/
class C1{
void printC1(String s){
System.out.println(s);
}
}
class C2{
void printC2(String s){
System.out.println(s);
}
}
public class Main{
static int cond=0;
public static void main(String []a){
C1 c=new C1();
C2 d=new C2();
Object lock= new Object();
new Thread(){
public void run(){
for(int i=0; i<5; i++){
c.printC1("A");
}
synchronized(lock){
try{
Main.cond++;
while(Main.cond<2){
System.out.println("Sto aspettando 2");
lock.wait();
}
System.out.println("Sono partito 1");
lock.notify();
}catch(InterruptedException e){}
}
for(int i=0; i<5; i++){
c.printC1("B");
}
}
}.start();
new Thread(){
public void run(){
for(int i=0; i<5; i++){
d.printC2("C");
}
synchronized(lock){
try{
Main.cond++;
while(Main.cond<2){
System.out.println("Sto aspettando 1");
lock.wait();
}
System.out.println("partito 2");
lock.notify();
}catch(InterruptedException e){}
}
for(int i=0; i<5; i++){
d.printC2("D");
}
}
}.start();
}
}