T - the templated type to store in the bagpublic class ConcurrentBag<T> extends Object implements AutoCloseable
Note that items that are "borrowed" from the bag are not actually removed from any collection, so garbage collection will not occur even if the reference is abandoned. Thus care must be taken to "requite" borrowed objects otherwise a memory leak will result. Only the "remove" method can completely remove an object from the bag.
| Modifier and Type | Class and Description |
|---|---|
static class |
ConcurrentBag.BagEntry<T> |
| Constructor and Description |
|---|
ConcurrentBag()
Construct a ConcurrentBag with the specified listener.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(ConcurrentBag.BagEntry<T> bagEntry)
Add a new object to the bag for others to borrow.
|
ConcurrentBag.BagEntry<T> |
borrow(long timeout,
TimeUnit timeUnit)
The method will borrow a BagEntry from the bag, blocking for the
specified timeout if none are available.
|
void |
close()
Close the bag to further adds.
|
boolean |
remove(ConcurrentBag.BagEntry<T> bagEntry)
Remove a value from the bag.
|
void |
requite(ConcurrentBag.BagEntry<T> bagEntry)
This method will return a borrowed object to the bag.
|
int |
size()
Get the total number of items in the bag.
|
List<ConcurrentBag.BagEntry<T>> |
values()
This method provides a "snapshot" in time of the bag items.
|
public ConcurrentBag()
public ConcurrentBag.BagEntry<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(ConcurrentBag.BagEntry<T> bagEntry)
bagEntry - the value to return to the bagNullPointerException - if value is nullIllegalStateException - if the bagEntry was not borrowed from the bagpublic void add(ConcurrentBag.BagEntry<T> bagEntry)
bagEntry - an object to add to the bagpublic boolean remove(ConcurrentBag.BagEntry<T> bagEntry)
borrow(long, TimeUnit) or reserve(T)bagEntry - the value to removeIllegalStateException - if an attempt is made to remove an object
from the bag that was not borrowed or reserved firstpublic void close()
close in interface AutoCloseablepublic List<ConcurrentBag.BagEntry<T>> values()
reserve(T)
on items in the list, or understand the concurrency implications of
modifying items, before performing any action on them.public int size()
Copyright © 2023. All rights reserved.