-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadSafeBoundedQueue.java
87 lines (67 loc) · 2.49 KB
/
ThreadSafeBoundedQueue.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
package threads;
import java.util.Arrays;
/* Implement a BoundedQueue that supports a data structure which is of fixed size and circular,
i.e, insert at the end and the items are being pulled from front should be done in a circular fashion.
I.e, if the end position exceeds the size of the array the index of end should be
fixed appropriately to the beginning of the array. If the queue is full return an error
Notice the T, it makes the code to work for any type of items.
*/
public class ThreadSafeBoundedQueue<T> {
private int enqueueIndex;// Separate index to ensure enqueue happens at the end
private int dequeueIndex;// Separate index to ensure dequeue happens at the
// start
private T[] items;
private int count;
@SuppressWarnings("unchecked")
public ThreadSafeBoundedQueue(int size) {
// Todo implement the constructor to create a bounded queue of ssize size
enqueueIndex = 0;
dequeueIndex = 0;
items = (T[]) (new Object[size]);
}
// Returns 0 if insertion was successful -1 if it fails
public Integer insertItem(T item) {
// Write code to insert item at the 'end'
if (count == items.length)
return -1;
items[enqueueIndex] = item;
enqueueIndex = ++enqueueIndex == items.length ? 0 : enqueueIndex;
++count;
return 0;
}
public T getFront() {
// Write code to return the item at the front and remove that item from the
// queue
if (count == 0)
throw new IllegalStateException();
T item = items[dequeueIndex];
dequeueIndex = ++dequeueIndex == items.length ? 0 : dequeueIndex;
--count;
return item;
}
public String toString() {
return Arrays.toString(items);
}
public static void main(String[] args) {
ThreadSafeBoundedQueue<Integer> NewQueue = new ThreadSafeBoundedQueue<Integer>(4);
NewQueue.insertItem(9);
NewQueue.insertItem(5);
NewQueue.insertItem(6);
NewQueue.insertItem(7);
System.out.println(NewQueue);
System.out.println(NewQueue.getFront());
System.out.println(NewQueue.getFront());
System.out.println(NewQueue);
NewQueue.insertItem(11);
System.out.println(NewQueue);
NewQueue.insertItem(12);
System.out.println(NewQueue);
System.out.println(NewQueue.getFront());
System.out.println(NewQueue.getFront());
System.out.println(NewQueue.getFront());
System.out.println(NewQueue);
NewQueue.insertItem(13);
NewQueue.insertItem(14);
System.out.println(NewQueue);
}
}