-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectFIFO.java
More file actions
155 lines (115 loc) · 2.92 KB
/
Copy pathObjectFIFO.java
File metadata and controls
155 lines (115 loc) · 2.92 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
154
155
package threadbook.ch18;
public class ObjectFIFO extends Object {
private Object[] queue;
private int capacity;
private int size;
private int head;
private int tail;
public ObjectFIFO(int cap) {
capacity = ( cap > 0 ) ? cap : 1; // at least 1
queue = new Object[capacity];
head = 0;
tail = 0;
size = 0;
}
public int getCapacity() {
return capacity;
}
public synchronized int getSize() {
return size;
}
public synchronized boolean isEmpty() {
return ( size == 0 );
}
public synchronized boolean isFull() {
return ( size == capacity );
}
public synchronized void add(Object obj)
throws InterruptedException {
waitWhileFull();
queue[head] = obj;
head = ( head + 1 ) % capacity;
size++;
notifyAll(); // let any waiting threads know about change
}
public synchronized void addEach(Object[] list)
throws InterruptedException {
//
// You might want to code a more efficient
// implementation here ... (see ByteFIFO.java)
//
for ( int i = 0; i < list.length; i++ ) {
add(list[i]);
}
}
public synchronized Object remove()
throws InterruptedException {
waitWhileEmpty();
Object obj = queue[tail];
// don't block GC by keeping unnecessary reference
queue[tail] = null;
tail = ( tail + 1 ) % capacity;
size--;
notifyAll(); // let any waiting threads know about change
return obj;
}
public synchronized Object[] removeAll()
throws InterruptedException {
//
// You might want to code a more efficient
// implementation here ... (see ByteFIFO.java)
//
Object[] list = new Object[size]; // use the current size
for ( int i = 0; i < list.length; i++ ) {
list[i] = remove();
}
// if FIFO was empty, a zero-length array is returned
return list;
}
public synchronized Object[] removeAtLeastOne()
throws InterruptedException {
waitWhileEmpty(); // wait for a least one to be in FIFO
return removeAll();
}
public synchronized boolean waitUntilEmpty(long msTimeout)
throws InterruptedException {
if ( msTimeout == 0L ) {
waitUntilEmpty(); // use other method
return true;
}
// wait only for the specified amount of time
long endTime = System.currentTimeMillis() + msTimeout;
long msRemaining = msTimeout;
while ( !isEmpty() && ( msRemaining > 0L ) ) {
wait(msRemaining);
msRemaining = endTime - System.currentTimeMillis();
}
// May have timed out, or may have met condition,
// calc return value.
return isEmpty();
}
public synchronized void waitUntilEmpty()
throws InterruptedException {
while ( !isEmpty() ) {
wait();
}
}
public synchronized void waitWhileEmpty()
throws InterruptedException {
while ( isEmpty() ) {
wait();
}
}
public synchronized void waitUntilFull()
throws InterruptedException {
while ( !isFull() ) {
wait();
}
}
public synchronized void waitWhileFull()
throws InterruptedException {
while ( isFull() ) {
wait();
}
}
}