Class ReferenceManager<G>

  • Type Parameters:
    G - the concrete type that will be acquired and released.
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    ReaderManager, SearcherManager, SearcherTaxonomyManager

    public abstract class ReferenceManager<G>
    extends java.lang.Object
    implements java.io.Closeable
    Utility class to safely share instances of a certain type across multiple threads, while periodically refreshing them. This class ensures each reference is closed only once all threads have finished using it. It is recommended to consult the documentation of ReferenceManager implementations for their maybeRefresh() semantics.
    • Constructor Detail

      • ReferenceManager

        public ReferenceManager()
    • Method Detail

      • acquire

        public final G acquire()
                        throws java.io.IOException
        Obtain the current reference. You must match every call to acquire with one call to 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.
        Throws:
        AlreadyClosedException - if the reference manager has been closed.
        java.io.IOException
      • close

        public final void close()
                         throws java.io.IOException

        Closes this ReferenceManager to prevent future acquiring. A reference manager should be closed if the reference to the managed resource should be disposed or the application using the ReferenceManager is shutting down. The managed resource might not be released immediately, if the ReferenceManager user is holding on to a previously acquired reference. The resource will be released once when the last reference is released. Those references can still be used as if the manager was still active.

        Applications should not acquire new references from this manager once this method has been called. Acquiring a resource on a closed ReferenceManager will throw an AlreadyClosedException.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException - if the underlying reader of the current reference could not be closed
      • maybeRefresh

        public final boolean maybeRefresh()
                                   throws java.io.IOException
        You must call this (or 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.

        Throws:
        java.io.IOException - if refreshing the resource causes an IOException
        AlreadyClosedException - if the reference manager has been closed.
      • maybeRefreshBlocking

        public final void maybeRefreshBlocking()
                                        throws java.io.IOException
        You must call this (or 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().

        Throws:
        java.io.IOException - if refreshing the resource causes an IOException
        AlreadyClosedException - if the reference manager has been closed.
      • release

        public final void release​(G reference)
                           throws java.io.IOException
        Release the reference previously obtained via acquire().

        NOTE: it's safe to call this after close().

        Throws:
        java.io.IOException - if the release operation on the given resource throws an IOException