Package htsjdk.io

Class AsyncWriterPool

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class AsyncWriterPool
    extends Object
    implements Closeable
    Implementation of an asynchronous writer pool.

    This creates a writer pool allowing for M writers and N threads where writers are not tied to an individual thread. This introduces a small amount of overhead compared to a writer-per-thread model and is best suited to scenarios where M is greater than M.

    • Constructor Detail

      • AsyncWriterPool

        public AsyncWriterPool​(int threads)
        Create an AsyncWriterPool using the specified number of threads. The number of threads in use at one time will grow and shrink with load.

        Note, any calls to the pool, or any created writers should come from the same thread. Behavior of calling methods on either AsyncWriterPool or its associated Writers from different threads is undefined.

        Parameters:
        threads - max number of threads to use
      • AsyncWriterPool

        public AsyncWriterPool()
        Create an AsyncWriterPool using all available processors. The number of threads in use at one time will grow and shrink with load.
    • Method Detail

      • close

        public void close()
                   throws IOException
        Asynchronously closes each writer in the pool. Each writer calls .close() in a CompletableFuture, which allows the writers to wait for any ongoing writes, drain any remaining elements from the queue, and then close the inner writer. All writers will immediately cease to accept new items to write and any call to .write() after calling this method will throw an exception. This method waits for all CompletableFutures to complete and then shuts down the executor.

        This method blocks and till all writers have closed and pool has shut down.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - if any writer raises an exception
      • getTimeoutSeconds

        public int getTimeoutSeconds()
        Get the timeoutSeconds value.
        Returns:
        the number of seconds a writer will wait to emplace an item in a queue for writing.
      • setTimeoutSeconds

        public void setTimeoutSeconds​(int timeoutSeconds)
        Set the timeoutSeconds value. timeoutSeconds is used by the writers to determine how long they should wait when trying to place an item in the queue for writing. This timeout only comes into play if the writer has failed and prevents a call to write from hanging.
        Parameters:
        timeoutSeconds - the number of senconds a writer will wait to emplace an item in a queue for writing.
      • pool

        public <A> Writer<A> pool​(Writer<A> writer,
                                  BlockingQueue<A> queue,
                                  int writeThreshold)
        Exchange a class implementing Writer for a AsyncWriterPool.PooledWriter.
        Type Parameters:
        A - the type of the items the Writer can write.
        Parameters:
        writer - a class implementing Writer
        queue - a queue to use for this writer, bounded or unbounded.
        writeThreshold - the minimum number of items needed before scheduling a thread for writing.
        Returns:
        a AsyncWriterPool.PooledWriter