-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
executable file
·44 lines (40 loc) · 1.39 KB
/
Client.java
File metadata and controls
executable file
·44 lines (40 loc) · 1.39 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
public class Client extends Thread{
private static int NUM_CLIENT = 0;
private final int MyId;
private final int res;
private int received;
private Server s;
public Client(String name, Server s, int res){
super(name);
this.s = s;
MyId = NUM_CLIENT;
NUM_CLIENT++;
this.res = res;
this.start();
}
public int getMyId(){
return MyId;
}
public void prologue(){
if (res == 0){
System.out.println(Thread.currentThread().getName() + ((Client)Thread.currentThread()).getMyId() + " request RA");
s.RArequest();
received = s.RAuse();
} else if (res == 1) {
System.out.println(Thread.currentThread().getName() + ((Client)Thread.currentThread()).getMyId() + " request RB");
s.RBrequest();
received = s.RBuse();
} else {
System.out.println(Thread.currentThread().getName() + ((Client)Thread.currentThread()).getMyId() + " request Any");
s.requestAny();
received = s.useAny();
}
}
public void run(){
prologue();
System.out.println(Thread.currentThread().getName() + ((Client)Thread.currentThread()).getMyId() + " using " + ((received == 0)?"RA":"RB"));
try { Thread.sleep(3000); } catch (Exception ex){}; /* simulating the use of the resource */
System.out.println(Thread.currentThread().getName() + ((Client)Thread.currentThread()).getMyId() + " release " + ((received == 0)?"RA":"RB"));
s.release(received);
}
}