



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




LinkedQueue
head
last
object
next
object
next
object
next
(unused)
LinkedQueue
head last
object
next
(unused)
class LinkedQueue { protected Node head = new Node(null); protected Node last = head;
static class Node { Object object; Node next = null; Node (Object x) { object = x; } }
synchronized Object tryToDequeue() { Object x = null; Node first = head.next; if (first != null) { x = first.object; first.object = null; head = first; } return x; }
synchronized Object dequeue() throws InterruptedException { while (head.next == null) wait(); Node first = head.next; Object x = first.object; first.object = null; head = first; return x; }
synchronized void acquireWriteLock() throws InterruptedException { while (writeLocked || readLocks > 0) wait(); writeLocked = true; }
synchronized void releaseWriteLock() { writeLocked = false; notifyAll(); }