2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.util.concurrent;
11 import com.google.common.annotations.Beta;
13 import java.util.Collection;
14 import java.util.concurrent.LinkedBlockingQueue;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 * A {@link LinkedBlockingQueue} that tracks the largest queue size for debugging.
21 * @author Thomas Pantelis
23 * @param <E> the element t.ype
25 public class TrackingLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> {
26 @SuppressWarnings("rawtypes")
27 private static final AtomicIntegerFieldUpdater<TrackingLinkedBlockingQueue> LARGEST_QUEUE_SIZE_UPDATER = AtomicIntegerFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, "largestQueueSize");
28 private static final long serialVersionUID = 1L;
31 * Holds largestQueueSize, this long field should be only accessed
32 * using {@link #LARGEST_QUEUE_SIZE_UPDATER}
34 private volatile int largestQueueSize = 0;
37 * @see LinkedBlockingQueue#LinkedBlockingQueue
39 public TrackingLinkedBlockingQueue() {
44 * @see LinkedBlockingQueue#LinkedBlockingQueue(Collection)
46 public TrackingLinkedBlockingQueue( final Collection<? extends E> c ) {
51 * @see LinkedBlockingQueue#LinkedBlockingQueue(int)
53 public TrackingLinkedBlockingQueue( final int capacity ) {
58 * Returns the largest queue size.
60 * FIXME: the this return will be changed to int in a future release.
63 public long getLargestQueueSize() {
64 return largestQueueSize;
68 public boolean offer( final E e, final long timeout, final TimeUnit unit ) throws InterruptedException {
69 if (super.offer( e, timeout, unit ) ) {
70 updateLargestQueueSize();
78 public boolean offer( final E e ) {
79 if (super.offer( e ) ) {
80 updateLargestQueueSize();
88 public void put( final E e ) throws InterruptedException {
90 updateLargestQueueSize();
94 public boolean add( final E e ) {
95 boolean result = super.add( e );
96 updateLargestQueueSize();
101 public boolean addAll( final Collection<? extends E> c ) {
103 return super.addAll( c );
105 updateLargestQueueSize();
109 private void updateLargestQueueSize() {
110 final int size = size();
114 largest = largestQueueSize;
115 } while (size > largest && !LARGEST_QUEUE_SIZE_UPDATER.weakCompareAndSet(this, largest, size));