Class IntolerantThreadPoolExecutor<T>
- Type Parameters:
T
- The type of the object being created by the threads run
- All Implemented Interfaces:
AutoCloseable
,Executor
,ExecutorService
,Spliterator<T>
- Direct Known Subclasses:
BeanExecutor
,LineExecutor
This is the historically established precedent for dealing with input errors
in opencsv. This implementation expects all uncaught exceptions from its
threads to be wrapped in a RuntimeException
. The number of
threads in the pool is fixed.
It is not intended for this executor to be instantiated and receive jobs directly. There are function-specific derived classes for that purpose.
This executor adds significant logic to the basic idea of an
Executor
, and thus must be used differently
from other executors. Usage follows this pattern:
prepare()
- Submit tasks. This is not intended to be done directly to this class, but rather to one of the submission methods of the derived classes.
complete()
- The results are had by creating a
Stream
out of the executor itself. This is most easily done withStreamSupport.stream(Spliterator, boolean)
- Possibly
getCapturedExceptions()
The execution structure of this class is:
- The main thread (outside of this executor) parses input and passes it on to
- This executor, which performs a number of conversions in parallel and passes these results and any resultant errors to
- The accumulator, which creates an ordered list of the results.
The threads in the executor queue their results in a thread-safe queue, which should be O(1), minimizing wait time due to synchronization. The accumulator then removes items from the queue and inserts them into a sorted data structure, which is O(log n) on average and O(n) in the worst case. If the user has told us she doesn't need sorted data, the accumulator is not necessary, and thus is not started.
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
Nested classes/interfaces inherited from interface java.util.Spliterator
Spliterator.OfDouble, Spliterator.OfInt, Spliterator.OfLong, Spliterator.OfPrimitive<T,
T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AccumulateCsvResults
<T> A separate thread that accumulates and orders results.protected final Locale
The locale for error messages.A list of the ordinals of data records still to be expected by the accumulator.private final boolean
Determines whether resulting data sets have to be in the same order as the input.private ConcurrentNavigableMap
<Long, T> A sorted, concurrent map for the beans created.protected final BlockingQueue
<OrderedObject<T>> A queue of the beans created.private Throwable
The exception that caused this Executor to stop executing.private org.apache.commons.collections4.ListValuedMap
<Long, CsvException> A multi-valued map for any exceptions captured.protected final BlockingQueue
<OrderedObject<CsvException>> A queue of exceptions thrown by threads during processing.Fields inherited from interface java.util.Spliterator
CONCURRENT, DISTINCT, IMMUTABLE, NONNULL, ORDERED, SIZED, SORTED, SUBSIZED
-
Constructor Summary
ConstructorsConstructorDescriptionIntolerantThreadPoolExecutor
(boolean orderedResults, Locale errorLocale) Constructor for a thread pool executor that stops by itself as soon as any thread throws an exception. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
afterExecute
(Runnable r, Throwable t) Shuts the Executor down if the thread ended in an exception.private boolean
Determines whether more conversion results can be expected.int
protected void
Checks whether exceptions are available that should halt processing.void
complete()
Sends a signal to the Executor that it should shut down once all threads have completed.long
Returns exceptions captured during the conversion process if the conversion process was set not to propagate these errors up the call stack.If an unrecoverable exception was thrown during processing, it can be retrieved here.private boolean
void
prepare()
Prepares this Executor to receive jobs.boolean
tryAdvance
(Consumer<? super T> action) trySplit()
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, execute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, terminated, toString
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ExecutorService
close
Methods inherited from interface java.util.Spliterator
forEachRemaining, getComparator, getExactSizeIfKnown, hasCharacteristics
-
Field Details
-
resultQueue
A queue of the beans created. -
thrownExceptionsQueue
A queue of exceptions thrown by threads during processing. -
resultantBeansMap
A sorted, concurrent map for the beans created. -
thrownExceptionsMap
A multi-valued map for any exceptions captured.The multi-valued part is important because the same line can throw more than one exception.
All access to this variable must be synchronized.
-
accumulateThread
A separate thread that accumulates and orders results. -
expectedRecords
A list of the ordinals of data records still to be expected by the accumulator. -
orderedResults
private final boolean orderedResultsDetermines whether resulting data sets have to be in the same order as the input. -
errorLocale
The locale for error messages. -
terminalException
The exception that caused this Executor to stop executing.
-
-
Constructor Details
-
IntolerantThreadPoolExecutor
IntolerantThreadPoolExecutor(boolean orderedResults, Locale errorLocale) Constructor for a thread pool executor that stops by itself as soon as any thread throws an exception. Threads never time out and the queue for inbound work is unbounded.- Parameters:
orderedResults
- Whether order should be preserved in the resultserrorLocale
- The errorLocale to use for error messages.
-
-
Method Details
-
prepare
public void prepare()Prepares this Executor to receive jobs. -
complete
Sends a signal to the Executor that it should shut down once all threads have completed.- Throws:
InterruptedException
- If the current thread is interrupted while waiting. Shouldn't be thrown, since the Executor waits indefinitely for all threads to end.RejectedExecutionException
- If an exception during processing forced this Executor to shut down.
-
getCapturedExceptions
Returns exceptions captured during the conversion process if the conversion process was set not to propagate these errors up the call stack. The call is nondestructive.- Returns:
- All exceptions captured
-
shutdownNow
- Specified by:
shutdownNow
in interfaceExecutorService
- Overrides:
shutdownNow
in classThreadPoolExecutor
-
afterExecute
Shuts the Executor down if the thread ended in an exception.- Overrides:
afterExecute
in classThreadPoolExecutor
- Parameters:
r
-t
-
-
getTerminalException
If an unrecoverable exception was thrown during processing, it can be retrieved here.- Returns:
- The exception that halted one of the threads, which caused the executor to shut itself down
-
checkExceptions
protected void checkExceptions()Checks whether exceptions are available that should halt processing. This is the case with unrecoverable errors, such as parsing the input, or if exceptions in conversion should be thrown by request of the user. -
isConversionComplete
private boolean isConversionComplete() -
areMoreResultsAvailable
private boolean areMoreResultsAvailable()Determines whether more conversion results can be expected. SinceSpliterator
s have no way of indicating that they don't have a result at the moment, but might in the future, we must ensure that every call totryAdvance(Consumer)
ortrySplit()
only returnsnull
if the entire conversion apparatus has shut down and all result queues are cleared. Thus, this method waits until either that is true, or there is truly at least one result that can be returned to users of theSpliterator
interface.- Returns:
false
if conversion is complete and no more results can ever be expected out of thisSpliterator
,true
otherwise. Iftrue
is returned, it is guaranteed that at least one result is available immediately to the caller.
-
tryAdvance
- Specified by:
tryAdvance
in interfaceSpliterator<T>
-
trySplit
- Specified by:
trySplit
in interfaceSpliterator<T>
-
estimateSize
public long estimateSize()- Specified by:
estimateSize
in interfaceSpliterator<T>
-
characteristics
public int characteristics()- Specified by:
characteristics
in interfaceSpliterator<T>
-