T
- the templated type to store in the bagpublic class ConcurrentBag<T extends ConcurrentBag.IBagManagable> extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
ConcurrentBag.IBagManagable
This interface must be implemented by classes wishing to be managed by
ConcurrentBag.
|
static interface |
ConcurrentBag.IBagStateListener
This interface is implemented by a listener to the bag.
|
Modifier and Type | Field and Description |
---|---|
static int |
STATE_IN_USE |
static int |
STATE_NOT_IN_USE |
Constructor and Description |
---|
ConcurrentBag()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(T value)
Add a new object to the bag for others to borrow.
|
void |
addBagStateListener(ConcurrentBag.IBagStateListener listener)
Add a listener to the bag.
|
T |
borrow(long timeout,
TimeUnit timeUnit)
The method will borrow an IBagManagable from the bag, blocking for the
specified timeout if none are available.
|
int |
getCount(int state) |
int |
getPendingQueue()
Get the number of threads pending (waiting) for an item from the
bag to become available.
|
void |
remove(T value)
Remove a value from the bag.
|
void |
requite(T value)
This method will return a borrowed object to the bag.
|
boolean |
reserve(T value)
The method is used to make an item in the bag "unavailable" for
borrowing.
|
int |
size()
Get the total number of items in the bag.
|
void |
unreserve(T value)
This method is used to make an item reserved via
reserve(IBagManagable)
available again for borrowing. |
List<T> |
values(int state)
This method provides a "snaphot" in time of the IBagManagable
items in the bag in the specified state.
|
public static final int STATE_NOT_IN_USE
public static final int STATE_IN_USE
public T borrow(long timeout, TimeUnit timeUnit) throws InterruptedException
timeout
- how long to wait before giving up, in units of unittimeUnit
- a TimeUnit
determining how to interpret the timeout parameterInterruptedException
- if interrupted while waitingpublic void requite(T value)
value
- the value to return to the bagNullPointerException
- if value is nullIllegalStateException
- if the requited value was not borrowed from the bagpublic void add(T value)
value
- an object to add to the bagpublic void remove(T value)
borrow(long, TimeUnit)
or reserve(IBagManagable)
.value
- the value to removeIllegalStateException
- if an attempt is made to remove an object
from the bag that was not borrowed or reserved firstpublic List<T> values(int state)
reserve(IBagManagable)
on items in list before performing any action on them.state
- one of STATE_NOT_IN_USE or STATE_IN_USEpublic boolean reserve(T value)
values(int)
method. Items that are
reserved can be removed from the bag via remove(IBagManagable)
without the need to unreserve them. Items that are not removed
from the bag can be make available for borrowing again by calling
the unreserve(IBagManagable)
method.value
- the item to reservepublic void unreserve(T value)
reserve(IBagManagable)
available again for borrowing.value
- the item to unreservepublic void addBagStateListener(ConcurrentBag.IBagStateListener listener)
listener
- a listener to the bagpublic int getPendingQueue()
public int getCount(int state)
public int size()
Copyright © 2014 Zaxxer.com. All rights reserved.