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
8 package org.opendaylight.yangtools.util.concurrent;
10 import com.google.common.annotations.Beta;
11 import java.util.Collection;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 import org.eclipse.jdt.annotation.NonNull;
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> {
25 private static final long serialVersionUID = 1L;
27 @SuppressWarnings("rawtypes")
28 private static final AtomicIntegerFieldUpdater<TrackingLinkedBlockingQueue> LARGEST_QUEUE_SIZE_UPDATER
29 = AtomicIntegerFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, "largestQueueSize");
32 * Holds largestQueueSize, this long field should be only accessed
33 * using {@link #LARGEST_QUEUE_SIZE_UPDATER}.
35 private volatile int largestQueueSize = 0;
38 * See {@link LinkedBlockingQueue#LinkedBlockingQueue()}.
40 public TrackingLinkedBlockingQueue() {
44 * See {@link LinkedBlockingQueue#LinkedBlockingQueue(Collection)}.
46 @SuppressWarnings("checkstyle:parameterName")
47 public TrackingLinkedBlockingQueue(final @NonNull Collection<? extends E> c) {
52 * See {@link LinkedBlockingQueue#LinkedBlockingQueue(int)}.
54 public TrackingLinkedBlockingQueue(final int capacity) {
59 * Returns the largest queue size.
61 * <p>FIXME: the this return will be changed to int in a future release.
64 public long getLargestQueueSize() {
65 return largestQueueSize;
69 @SuppressWarnings("checkstyle:parameterName")
70 public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException {
71 if (super.offer(e, timeout, unit)) {
72 updateLargestQueueSize();
80 @SuppressWarnings("checkstyle:parameterName")
81 public boolean offer(final E e) {
83 updateLargestQueueSize();
91 @SuppressWarnings("checkstyle:parameterName")
92 public void put(final E e) throws InterruptedException {
94 updateLargestQueueSize();
98 @SuppressWarnings("checkstyle:parameterName")
99 public boolean add(final E e) {
100 boolean result = super.add(e);
101 updateLargestQueueSize();
106 @SuppressWarnings("checkstyle:parameterName")
107 public boolean addAll(final Collection<? extends E> c) {
109 return super.addAll(c);
111 updateLargestQueueSize();
115 private void updateLargestQueueSize() {
116 final int size = size();
120 largest = largestQueueSize;
121 } while (size > largest && !LARGEST_QUEUE_SIZE_UPDATER.weakCompareAndSet(this, largest, size));