public abstract class ReferenceManager<G> extends Object implements Closeable
ReferenceManager
implementations for their maybeRefresh()
semantics.Constructor and Description |
---|
ReferenceManager() |
Modifier and Type | Method and Description |
---|---|
G |
acquire()
Obtain the current reference.
|
protected void |
afterClose()
Called after close(), so subclass can free any resources.
|
protected void |
afterRefresh()
Called after swapReference has installed a new
instance.
|
void |
close()
Close this ReferenceManager to future
acquiring . |
protected abstract void |
decRef(G reference)
Decrement reference counting on the given reference.
|
boolean |
maybeRefresh()
You must call this (or
maybeRefreshBlocking() ), periodically, if
you want that acquire() will return refreshed instances. |
void |
maybeRefreshBlocking()
You must call this (or
maybeRefresh() ), periodically, if you want
that acquire() will return refreshed instances. |
protected abstract G |
refreshIfNeeded(G referenceToRefresh)
Refresh the given reference if needed.
|
void |
release(G reference)
Release the refernce previously obtained via
acquire() . |
protected abstract boolean |
tryIncRef(G reference)
Try to increment reference counting on the given reference.
|
protected volatile G current
protected abstract void decRef(G reference) throws IOException
IOException
protected abstract G refreshIfNeeded(G referenceToRefresh) throws IOException
null
if no refresh
was needed, otherwise a new refreshed reference.IOException
protected abstract boolean tryIncRef(G reference)
public final G acquire()
release(G)
; it's best to do so in a finally clause, and set
the reference to null
to prevent accidental usage after it has been
released.public final void close() throws IOException
acquiring
. Any
references that were previously acquired
won't be
affected, and they should still be released
when they are
not needed anymore.close
in interface Closeable
IOException
protected void afterClose() throws IOException
IOException
public final boolean maybeRefresh() throws IOException
maybeRefreshBlocking()
), periodically, if
you want that acquire()
will return refreshed instances.
Threads: it's fine for more than one thread to call this at once. Only the first thread will attempt the refresh; subsequent threads will see that another thread is already handling refresh and will return immediately. Note that this means if another thread is already refreshing then subsequent threads will return right away without waiting for the refresh to complete.
If this method returns true it means the calling thread either refreshed or that there were no changes to refresh. If it returns false it means another thread is currently refreshing.
IOException
public final void maybeRefreshBlocking() throws IOException
maybeRefresh()
), periodically, if you want
that acquire()
will return refreshed instances.
Threads: unlike maybeRefresh()
, if another thread is
currently refreshing, this method blocks until that thread completes. It is
useful if you want to guarantee that the next call to acquire()
will return a refreshed instance. Otherwise, consider using the
non-blocking maybeRefresh()
.
IOException
protected void afterRefresh() throws IOException
IOException
public final void release(G reference) throws IOException
IOException
Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.