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 java.util.Collection;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.atomic.AtomicLong;
15 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
18 * A {@link LinkedBlockingQueue} that tracks the largest queue size for debugging.
20 * @author Thomas Pantelis
22 * @param <E> the element t.ype
24 public class TrackingLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> {
26 private static final long serialVersionUID = 1L;
29 * Holds largestQueueSize, this long field should be only accessed
30 * using {@value #LARGEST_QUEUE_SIZE_UPDATER}
32 private volatile long largestQueueSize = 0;
34 @SuppressWarnings("rawtypes")
35 private static AtomicLongFieldUpdater<TrackingLinkedBlockingQueue> LARGEST_QUEUE_SIZE_UPDATER = AtomicLongFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, "largestQueueSize");
38 * @see LinkedBlockingQueue#LinkedBlockingQueue
40 public TrackingLinkedBlockingQueue() {
45 * @see LinkedBlockingQueue#LinkedBlockingQueue(Collection)
47 public TrackingLinkedBlockingQueue( Collection<? extends E> c ) {
52 * @see LinkedBlockingQueue#LinkedBlockingQueue(int)
54 public TrackingLinkedBlockingQueue( int capacity ) {
59 * Returns the largest queue size.
61 public long getLargestQueueSize(){
62 return largestQueueSize;
66 public boolean offer( E e, long timeout, TimeUnit unit ) throws InterruptedException {
67 if( super.offer( e, timeout, unit ) ) {
68 updateLargestQueueSize();
76 public boolean offer( E e ) {
77 if( super.offer( e ) ) {
78 updateLargestQueueSize();
86 public void put( E e ) throws InterruptedException {
88 updateLargestQueueSize();
92 public boolean add( E e ) {
93 boolean result = super.add( e );
94 updateLargestQueueSize();
99 public boolean addAll( Collection<? extends E> c ) {
101 return super.addAll( c );
103 updateLargestQueueSize();
107 private void updateLargestQueueSize() {
109 long largest = largestQueueSize;
110 if( size > largest ) {
111 LARGEST_QUEUE_SIZE_UPDATER.compareAndSet(this, largest, size );