-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedData.java
62 lines (54 loc) · 1.19 KB
/
SharedData.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
import java.util.*;
public class SharedData
{
ArrayList<Integer>al=new ArrayList<Integer>();
public boolean gameCompleteFlag ;
private int contents;
private int available;
public SharedData()
{
gameCompleteFlag=false;
available=0;
contents=0;
}
/* public int avl()
{
return available;
}*/
public synchronized int get()
{
while (available == 0)
{
try
{
wait();
}
catch (InterruptedException e) {
System.out.println("COULDN'T WAIT");
}
}
//System.out.println("available to be updated " +available );
available-- ;
notifyAll();
return contents;
}
public synchronized void put(int value)
{
while (available != 0)
{
try {
wait();
}
catch (InterruptedException e) { }
// continue;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
// System.out.println("WAIT");
contents=value;
available = 2;
al.add(value);
notifyAll();
}
}