SynchronousQueue
( Taken from another question )SynchronousQueue
is more of a handoff, whereas the LinkedBlockingQueue
just allows a single element. The difference being that the put()
call to a SynchronousQueue
will not return until there is a corresponding take()
call, but with a LinkedBlockingQueue
of size 1, the put()
call (to an empty queue) will return immediately. It's essentially the BlockingQueue
implementation for when you don't really want a queue (you don't want to maintain any pending data).
LinkedBlockingQueue
(LinkedList
Implementation but Not Exactly JDK Implementation of LinkedList
It uses static inner class Node to maintain Links between elements )Constructor for LinkedBlockingQueue
public LinkedBlockingQueue(int capacity)
{
if (capacity < = 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node< E >(null); // Maintains a underlying linkedlist. ( Use when size is not known )
}
Node class Used to Maintain Links
static class Node<E> {
E item;
Node<E> next;
Node(E x) { item = x; }
}
3 . ArrayBlockingQueue ( Array Implementation )
Constructor for ArrayBlockingQueue
public ArrayBlockingQueue(int capacity, boolean fair)
{
if (capacity < = 0)
throw new IllegalArgumentException();
this.items = new Object[capacity]; // Maintains a underlying array
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
IMHO Biggest Difference between ArrayBlockingQueue
and LinkedBlockingQueue
is clear from constructor one has underlying data structure Array and other linkedList.
ArrayBlockingQueue
uses single-lock double condition algorithm and LinkedBlockingQueue
is variant of the "two lock queue" algorithm and it has 2 locks 2 conditions ( takeLock , putLock)