-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAgent.java
More file actions
48 lines (39 loc) · 805 Bytes
/
Agent.java
File metadata and controls
48 lines (39 loc) · 805 Bytes
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
package Smokers;
import java.util.Random;
public class Agent extends Thread{
public void run() {
int time = 0;
while(true) {
try {
S.nothing.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
S.mutex.acquire();
}catch(InterruptedException e){
e.printStackTrace();
}
if(time>5)
break;
place();
S.something.release();
S.mutex.release();
time++;
}
}
public void place() {
Random r =new Random();
StringBuilder result = new StringBuilder();
result.append(" agent put ");
int m = r.nextInt(3);
for(int i =0 ;i<3;i++) {
if(i!=m) {
S.smoke.get(i).set(true);
result.append(" "+S.name.get(i));
}
}
System.out.println(result.toString());
}
}