Package alex.mojaki.s3upload
Class ExecutorServiceResultsHandler<V>
- java.lang.Object
-
- alex.mojaki.s3upload.ExecutorServiceResultsHandler<V>
-
- All Implemented Interfaces:
Iterable<V>
public class ExecutorServiceResultsHandler<V> extends Object implements Iterable<V>
Wrapper around an ExecutorService that allows you to easily submitCallables, get results via iteration, and handle failure quickly. When a submitted callable throws an exception in its thread this will result in aRuntimeExceptionwhen iterating over results. Typical usage is as follows:- Create an ExecutorService and pass it to the constructor.
- Create Callables and ensure that they respond to interruption, e.g. regularly call:
if (Thread.currentThread().isInterrupted()) { throw new RuntimeException("The thread was interrupted, likely indicating failure in a sibling thread."); } - Pass the callables to the
submit()method. - Call
finishedSubmitting(). - Iterate over this object (e.g. with a foreach loop) to get results from the callables.
Each iteration will block waiting for the next result.
If one of the callables throws an unhandled exception or the thread is interrupted during iteration
then
ExecutorService.shutdownNow()will be called resulting in all still running callables being interrupted, and aRuntimeExceptionwill be thrown
You can also call
abort()to shut down the threads yourself.
-
-
Constructor Summary
Constructors Constructor Description ExecutorServiceResultsHandler(ExecutorService executorService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort()voidawaitCompletion()Convenience method to wait for the callables to finish for when you don't care about the results.voidfinishedSubmitting()Iterator<V>iterator()voidsubmit(Callable<V> task)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
ExecutorServiceResultsHandler
public ExecutorServiceResultsHandler(ExecutorService executorService)
-
-