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;
12 import java.util.Collection;
13 import java.util.concurrent.LinkedBlockingQueue;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
16 import javax.annotation.Nonnull;
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 private static final long serialVersionUID = 1L;
28 @SuppressWarnings("rawtypes")
29 private static final AtomicIntegerFieldUpdater<TrackingLinkedBlockingQueue> LARGEST_QUEUE_SIZE_UPDATER
30 = AtomicIntegerFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, "largestQueueSize");
33 * Holds largestQueueSize, this long field should be only accessed
34 * using {@link #LARGEST_QUEUE_SIZE_UPDATER}.
36 private volatile int largestQueueSize = 0;
39 * See {@link LinkedBlockingQueue#LinkedBlockingQueue()}.
41 public TrackingLinkedBlockingQueue() {
46 * See {@link LinkedBlockingQueue#LinkedBlockingQueue(Collection)}.
48 @SuppressWarnings("checkstyle:parameterName")
49 public TrackingLinkedBlockingQueue(final Collection<? extends E> c) {
54 * See {@link LinkedBlockingQueue#LinkedBlockingQueue(int)}.
56 public TrackingLinkedBlockingQueue(final int capacity) {
61 * Returns the largest queue size.
63 * <p>FIXME: the this return will be changed to int in a future release.
66 public long getLargestQueueSize() {
67 return largestQueueSize;
71 @SuppressWarnings("checkstyle:parameterName")
72 public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException {
73 if (super.offer(e, timeout, unit)) {
74 updateLargestQueueSize();
82 @SuppressWarnings("checkstyle:parameterName")
83 public boolean offer(@Nonnull final E e) {
85 updateLargestQueueSize();
93 @SuppressWarnings("checkstyle:parameterName")
94 public void put(final E e) throws InterruptedException {
96 updateLargestQueueSize();
100 @SuppressWarnings("checkstyle:parameterName")
101 public boolean add(final E e) {
102 boolean result = super.add(e);
103 updateLargestQueueSize();
108 @SuppressWarnings("checkstyle:parameterName")
109 public boolean addAll(final Collection<? extends E> c) {
111 return super.addAll(c);
113 updateLargestQueueSize();
117 private void updateLargestQueueSize() {
118 final int size = size();
122 largest = largestQueueSize;
123 } while (size > largest && !LARGEST_QUEUE_SIZE_UPDATER.weakCompareAndSet(this, largest, size));