-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp091216.java
169 lines (137 loc) · 3.7 KB
/
app091216.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.lang.Math; class
import java.util.Vector;
class Auto{
private int i;
private String direzione;
private static int n;
Auto(String dir){i=n++; direzione=dir;}
public String toString(){return " Auto n."+i+" proveniente da "+direzione;}
public void attraversa(){System.out.println("ora passa " +this);}
}
class Incrocio{
Vector<Auto> codaPrincipale=new Vector<Auto>();
Vector<Auto> codaSecondaria=new Vector<Auto>();
Object carlo= new Object();
public class GestoreStradaPrincipale extends Thread{
public void run(){
while(true){
synchronized(Incrocio.this){
try{
while(codaPrincipale.isEmpty()){
Incrocio.this.notify();
Incrocio.this.wait();
}
codaPrincipale.get(0).attraversa();
codaPrincipale.remove(0);
}catch(InterruptedException e){}
}
}
}
}
public class GestoreStradaSecondaria extends Thread{
public void run(){
while(true){
synchronized(Incrocio.this){
try{
while(codaSecondaria.isEmpty() || !codaPrincipale.isEmpty()){
Incrocio.this.wait();
}
synchronized(carlo){
(codaSecondaria.get(0)).attraversa();
codaSecondaria.remove(0);
}
}catch(InterruptedException e){}
}
}
}
}
public class GeneraAuto extends Thread{
public void run(){
for(int i=0; i<5; i++){
synchronized(carlo){
codaPrincipale.add(new Auto("coda princ"));
}
codaSecondaria.add(new Auto("coda sec"));
}
synchronized(Incrocio.this){
Incrocio.this.notifyAll();
}
try{
sleep(300);
}
catch(Exception e){}
}
}
}
public class Main{
public static void main(String[] a){
Incrocio i=new Incrocio();
Incrocio.GestoreStradaPrincipale gsp= i.new GestoreStradaPrincipale();
Incrocio.GestoreStradaSecondaria gss= i.new GestoreStradaSecondaria();
Incrocio.GeneraAuto ga=i.new GeneraAuto();
gsp.start(); gss.start(); ga.start();
}
}
////****roba server**//
interface R extends Remote{
void g(){}
void f(){}
}
class RImpl extends UnicastRemoteObjects implements Remote{
synchronized void f(){
System.out.println("FF")
System.out.println("FF")
System.out.println("FF")
System.out.println("FF")
System.out.println("FF")
}
void g(){
System.out.println("GG")
System.out.println("GG")
System.out.println("GG")
System.out.println("GG")
System.out.println("GG")
}
}
class Server{
public static void main(String []a){
R r=new RImpl();
Naming.rebind("r",r);
}
}
class C1{
void m(String s){
System.out.println(s);
System.out.println(s);
System.out.println(s);
System.out.println(s);
System.out.println(s);
}
public static void main(String []a){
R r=(R)Naming.lookup("r");
Thread t1=new Thread{
public void run(){
m("A");
synchronized(r){
r.f();
}
m("B");
}
}.start();
Thread t2=new Thread{
public void run(){
m("UNO");
synchronized(r){
r.g();
}
m("DUE");
}
}.start();
}
}
class C2{
public static void main(String []a){
R r=(R)Naming.lookup("r");
r.f();
}
}