Package org.jgroups.util
Class SizeBoundedQueue<T>
- java.lang.Object
-
- org.jgroups.util.SizeBoundedQueue<T>
-
public class SizeBoundedQueue<T> extends java.lang.Object
Blocking 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 class
SizeBoundedQueue.El<T>
-
Field Summary
Fields Modifier and Type Field Description protected int
count
protected boolean
done
protected java.util.concurrent.locks.Lock
lock
protected int
max_size
protected java.util.concurrent.locks.Condition
not_empty
protected java.util.concurrent.locks.Condition
not_full
protected java.util.Queue<SizeBoundedQueue.El<T>>
queue
protected int
waiters
-
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 void
add(T element, int size)
void
clear(boolean done)
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.int
getElements()
Returns the number of elements in the queueint
getWaiters()
boolean
hasWaiters()
boolean
isDone()
boolean
isEmpty()
int
queueSize()
For testing only - should always be the same as size()T
remove()
Removes and returns the first element or null if the queue is emptyint
size()
Returns the accumulated size of all elements in the queuejava.lang.String
toString()
-
-
-
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:
toString
in classjava.lang.Object
-
-