/** * Factory method to create a synchronized queue. * * @param <E> * the type of the elements in the queue * @param queue * the queue to decorate, must not be null * @return a new synchronized Queue * @throws NullPointerException * if queue is null */ public static <E> SynchronizedQueue<E> synchronizedQueue(final Queue<E> queue) { return new SynchronizedQueue<>(queue); }
@Override public int hashCode() { synchronized (lock) { return decorated().hashCode(); } }
/** * Returns a synchronized (thread-safe) queue backed by the given queue. * In order to guarantee serial access, it is critical that all access to the * backing queue is accomplished through the returned queue. * <p> * It is imperative that the user manually synchronize on the returned queue * when iterating over it: * * <pre> * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue()); * ... * synchronized(queue) { * Iterator i = queue.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * } * </pre> * * Failure to follow this advice may result in non-deterministic behavior. * * @param <E> the element type * @param queue the queue to synchronize, must not be null * @return a synchronized queue backed by that queue * @throws NullPointerException if the queue is null * @since 4.2 */ public static <E> Queue<E> synchronizedQueue(final Queue<E> queue) { return SynchronizedQueue.synchronizedQueue(queue); }
@Override public E poll() { synchronized (lock) { return decorated().poll(); } }
@Override public E peek() { synchronized (lock) { return decorated().peek(); } }
@Override public boolean equals(final Object object) { if (object == this) { return true; } synchronized (lock) { return decorated().equals(object); } }
@Override public boolean offer(final E e) { synchronized (lock) { return decorated().offer(e); } }
@Override public E remove() { synchronized (lock) { return decorated().remove(); } }
@Override public E element() { synchronized (lock) { return decorated().element(); } }