Package org.jgroups.util
Class SizeBoundedQueue<T>
- java.lang.Object
-
- org.jgroups.util.SizeBoundedQueue<T>
-
public class SizeBoundedQueue<T> extends java.lang.ObjectBlocking FIFO queue bounded by the max number of bytes of all elements. When adding threads are blocked due to capacity constraints, and the application terminates, it is the caller's duty to interrupt all threads.- Since:
- 4.0.4
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classSizeBoundedQueue.El<T>
-
Field Summary
Fields Modifier and Type Field Description protected intcountprotected booleandoneprotected java.util.concurrent.locks.Locklockprotected intmax_sizeprotected java.util.concurrent.locks.Conditionnot_emptyprotected java.util.concurrent.locks.Conditionnot_fullprotected java.util.Queue<SizeBoundedQueue.El<T>>queueprotected intwaiters
-
Constructor Summary
Constructors Constructor Description SizeBoundedQueue(int max_size)SizeBoundedQueue(int max_size, java.util.concurrent.locks.Lock lock)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(T element, int size)voidclear(boolean done)intdrainTo(java.util.Collection<T> c, int max_bytes)Removes and adds to collection c as many elements as possible (including waiters) but not exceeding max_bytes.intgetElements()Returns the number of elements in the queueintgetWaiters()booleanhasWaiters()booleanisDone()booleanisEmpty()intqueueSize()For testing only - should always be the same as size()Tremove()Removes and returns the first element or null if the queue is emptyintsize()Returns the accumulated size of all elements in the queuejava.lang.StringtoString()
-
-
-
Field Detail
-
lock
protected final java.util.concurrent.locks.Lock lock
-
not_full
protected final java.util.concurrent.locks.Condition not_full
-
not_empty
protected final java.util.concurrent.locks.Condition not_empty
-
max_size
protected final int max_size
-
queue
protected final java.util.Queue<SizeBoundedQueue.El<T>> queue
-
count
protected int count
-
waiters
protected int waiters
-
done
protected boolean done
-
-
Method Detail
-
add
public void add(T element, int size) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
remove
public T remove()
Removes and returns the first element or null if the queue is empty
-
drainTo
public int drainTo(java.util.Collection<T> c, int max_bytes)
Removes and adds to collection c as many elements as possible (including waiters) but not exceeding max_bytes. E.g. if we have elements {1000b, 2000b, 4000b} and max_bytes=6000, then only the first 2 elements are removed and the new size is 4000- Parameters:
c- The collection to transfer the removed elements tomax_bytes- The max number of bytes to remove- Returns:
- The accumulated number of bytes of all removed elements
-
clear
public void clear(boolean done)
-
getElements
public int getElements()
Returns the number of elements in the queue
-
size
public int size()
Returns the accumulated size of all elements in the queue
-
isEmpty
public boolean isEmpty()
-
getWaiters
public int getWaiters()
-
hasWaiters
public boolean hasWaiters()
-
isDone
public boolean isDone()
-
queueSize
public int queueSize()
For testing only - should always be the same as size()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-