Package htsjdk.io
Class AsyncWriterPool
java.lang.Object
htsjdk.io.AsyncWriterPool
- All Implemented Interfaces:
Closeable
,AutoCloseable
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 Summary
ConstructorDescriptionCreate an AsyncWriterPool using all available processors.AsyncWriterPool
(int threads) Create an AsyncWriterPool using the specified number ofthreads
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Asynchronously closes each writer in the pool.int
Get thetimeoutSeconds
value.<A> Writer<A>
pool
(Writer<A> writer, BlockingQueue<A> queue, int writeThreshold) Exchange a class implementingWriter
for aAsyncWriterPool.PooledWriter
.void
setTimeoutSeconds
(int timeoutSeconds) Set thetimeoutSeconds
value.
-
Constructor Details
-
AsyncWriterPool
public AsyncWriterPool(int threads) Create an AsyncWriterPool using the specified number ofthreads
. The number ofthreads
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 Details
-
close
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 allCompletableFuture
s 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if any writer raises an exception
-
getTimeoutSeconds
public int getTimeoutSeconds()Get thetimeoutSeconds
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 thetimeoutSeconds
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 towrite
from hanging.- Parameters:
timeoutSeconds
- the number of senconds a writer will wait to emplace an item in a queue for writing.
-
pool
Exchange a class implementingWriter
for aAsyncWriterPool.PooledWriter
.- Type Parameters:
A
- the type of the items theWriter
can write.- Parameters:
writer
- a class implementingWriter
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
-