Package htsjdk.io
Class AsyncWriterPool
- java.lang.Object
-
- htsjdk.io.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
Mwriters andNthreads 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 whereMis greater thanM.
-
-
Constructor Summary
Constructors Constructor Description AsyncWriterPool()Create an AsyncWriterPool using all available processors.AsyncWriterPool(int threads)Create an AsyncWriterPool using the specified number ofthreads.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Asynchronously closes each writer in the pool.intgetTimeoutSeconds()Get thetimeoutSecondsvalue.<A> Writer<A>pool(Writer<A> writer, BlockingQueue<A> queue, int writeThreshold)Exchange a class implementingWriterfor aAsyncWriterPool.PooledWriter.voidsetTimeoutSeconds(int timeoutSeconds)Set thetimeoutSecondsvalue.
-
-
-
Constructor Detail
-
AsyncWriterPool
public AsyncWriterPool(int threads)
Create an AsyncWriterPool using the specified number ofthreads. The number ofthreadsin 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 IOExceptionAsynchronously 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 allCompletableFutures to complete and then shuts down the executor.This method blocks and till all writers have closed and pool has shut down.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if any writer raises an exception
-
getTimeoutSeconds
public int getTimeoutSeconds()
Get thetimeoutSecondsvalue.- 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 thetimeoutSecondsvalue.timeoutSecondsis 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 towritefrom 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 implementingWriterfor aAsyncWriterPool.PooledWriter.- Type Parameters:
A- the type of the items theWritercan write.- Parameters:
writer- a class implementingWriterqueue- 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
-
-