A reference counted resource, for example one that is borrowed from a pool, that may be retained an additional number of times, and released a correspondent number of times, over its lifecycle.
The resource is typically implicitly retained when it is first created. It may be retained more times (thus incrementing its reference count) and released (thus decrementing its reference count), until the reference count goes to zero.
The general rules to use Retainable objects are the following:
Retainable has been obtained by calling a method, and the
caller code consumes it, then the caller code must call release().Retainable has been obtained by caller2 by calling a
method, and caller2 returns it without consuming it to caller1,
then caller2 must not call release(), since caller1 will.Retainable has been obtained as a method argument, the
receiver code must either:
Retainable synchronously within the method, in which case
release() must not be called.Retainable to some other method, in which case release()
must not be called.Retainable for later or asynchronous processing, for
example storing it in containers such as java.util.Collection s, or capturing
it in a lambda that is passed to another thread, etc., in which case retain()
must be called and a mechanism to call release() later or asynchronously
for this additional retain() must be arranged.| Modifier and Type | Class and Description |
|---|---|
| public static class | Retainable.ReferenceCounter
A reference count implementation for a |
| public static class | Retainable.Wrapper
A wrapper of |
| Modifier and Type | Method and Description |
|---|---|
| public default boolean | |
| public default boolean | Returns: true when the reference count goes to zero or if there was no reference count,
false otherwise.Releases this resource, potentially decrementing a reference count (if any). |
| public default void | retain()
Retains this resource, potentially incrementing a reference count if there are resources that will be released. |
| canRetain | back to summary |
|---|---|
| public default boolean canRetain() Returns whether this resource is referenced counted by calls to Implementations may decide that special resources are not not referenced counted (for example,
| |
| release | back to summary |
|---|---|
| public default boolean release() Releases this resource, potentially decrementing a reference count (if any).
| |
| retain | back to summary |
|---|---|
| public default void retain() Retains this resource, potentially incrementing a reference count if there are resources that will be released. | |
A reference count implementation for a Retainable resource.
The reference count is initialized to 1 when the resource is created,
and therefore it is implicitly retained and needs a call to release().
Additional calls to retain() must be matched by correspondent
calls to release().
When the reference count goes to zero, the resource may be pooled.
When the resource is acquired from the pool, acquire() should be
called to set the reference count to 1.
| Modifier and Type | Field and Description |
|---|---|
| private final AtomicInteger |
| Access | Constructor and Description |
|---|---|
| public | |
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public boolean | canRetain()
Overrides default org. Returns whether this resource is referenced counted by calls to |
| public int | |
| public boolean | Returns: whether this buffer is retainedReturns whether |
| public boolean | release()
Overrides default org. Releases this resource, potentially decrementing a reference count (if any). |
| public void | retain()
Overrides default org. Retains this resource, potentially incrementing a reference count if there are resources that will be released. |
| public String | |
| public boolean |
| references | back to summary |
|---|---|
| private final AtomicInteger references | |
| ReferenceCounter | back to summary |
|---|---|
| public ReferenceCounter() | |
| ReferenceCounter | back to summary |
|---|---|
| protected ReferenceCounter(int initialCount) | |
| acquire | back to summary |
|---|---|
| public void acquire() Updates the reference count from This method should only be used when this resource is acquired from a pool. | |
| canRetain | back to summary |
|---|---|
| public boolean canRetain() Overrides default org. Doc from org. Returns whether this resource is referenced counted by calls to Implementations may decide that special resources are not not referenced counted (for example,
| |
| get | back to summary |
|---|---|
public int get()
| |
| isRetained | back to summary |
|---|---|
| public boolean isRetained() Returns whether
| |
| release | back to summary |
|---|---|
| public boolean release() Overrides default org. Doc from org. Releases this resource, potentially decrementing a reference count (if any).
| |
| retain | back to summary |
|---|---|
| public void retain() Overrides default org. Doc from org. Retains this resource, potentially incrementing a reference count if there are resources that will be released.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| tryRetain | back to summary |
|---|---|
| public boolean tryRetain() | |
Retainable instances.
| Modifier and Type | Field and Description |
|---|---|
| private final Retainable |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | canRetain()
Overrides default org. Returns whether this resource is referenced counted by calls to |
| public Retainable | |
| public boolean | release()
Overrides default org. Releases this resource, potentially decrementing a reference count (if any). |
| public void | retain()
Overrides default org. Retains this resource, potentially incrementing a reference count if there are resources that will be released. |
| public String |
| wrapped | back to summary |
|---|---|
| private final Retainable wrapped | |
| Wrapper | back to summary |
|---|---|
| public Wrapper(Retainable wrapped) | |
| canRetain | back to summary |
|---|---|
| public boolean canRetain() Overrides default org. Doc from org. Returns whether this resource is referenced counted by calls to Implementations may decide that special resources are not not referenced counted (for example,
| |
| getWrapped | back to summary |
|---|---|
| public Retainable getWrapped() | |
| release | back to summary |
|---|---|
| public boolean release() Overrides default org. Doc from org. Releases this resource, potentially decrementing a reference count (if any).
| |
| retain | back to summary |
|---|---|
| public void retain() Overrides default org. Doc from org. Retains this resource, potentially incrementing a reference count if there are resources that will be released.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |