+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1add stack and queuecccc41d

master
869 B40 linesraw
1import java.util.concurrent.locks.ReentrantLock;
2public class UnboundedQueue<T> {
3
4  ReentrantLock enqLock, deqLock;
5  Node<T> head;
6  Node<T> tail;
7  int size;
8  public UnboundedQueue() {
9    head = new Node<T>(null);
10    tail = head;
11    enqLock = new ReentrantLock();
12    deqLock = new ReentrantLock();
13  }  
14  public T deq() throws EmptyException {
15    T result;
16    deqLock.lock();
17    try {
18      if (head.next == null) {
19        throw new EmptyException();
20      }
21      result = head.next.value;
22      head = head.next;
23    } finally {
24      deqLock.unlock();
25    }
26    return result;
27  }
28  public void enq(T x) {
29    if (x == null) throw new NullPointerException();
30    enqLock.lock();
31    try {
32      Node<T> e = new Node<T>(x);
33      tail.next = e;
34      tail = e;
35    } finally {
36      enqLock.unlock();
37    }
38  }
39
40}