I am using several threads to do some heavy (and error-prone) processing on a large data set. I require all threads to finish execution, regardless of whether they throw an exception or terminate normally (no value is returned), before the program can continue. I am using a CountDownLatch to achieve this, and an ExecutorService to actually run the jobs. I want the worker threads (let’s call them JobManager-s for the sake of argument) to notify the latch even if they throw an exception. A JobManager can take anywhere between a second and an hour to complete, and may fail at any time. The idea is to invoke the “finalizer” method of JobManager if an exception is thrown. Now, the ExecutorService likes to catch exceptions or to conceal the true origin of the ones it does not. I have though of a few ways around this, neither of which is satisfactory:
-
Use
ExecutorService#execute(Runnable r)rather thansubmit(Runnable r). I can do that since I do not care about the return value of theJobManager. I have provided a customThreadFactory, which attaches anUncaughtExceptionHandlerto each newly created thread. The problem with this approach is that whenUncaughtExceptionHandler#uncaughtException(Thread t, Throwable e)is invoked,t‘sRunnableis of typeThreadPoolExecutor$Worker, and not of typeJobManager, which prevents me from invoking the “finalizer” method. -
Use a custom
ExecutorServiceand override theafterExecute(Runnable r, Throwable t)method. This suffers from the same problem as 1. -
Wrap the whole
JobManager#doWork()in acatchstatement and use the return value to indicate if an exception was thrown. I can thensubmitthe jobs and useFutureTask#get()to decide if an exception was thrown. I do not like this solution because I feel return codes the wrong tool when you have an elaborate exception mechanism. Moreover,get()will wait (unless interrupted), which means I cannot handle errors in other threads immediately. -
Get rid of the
CountDownLatch. Store allFutures in a list and repeatedly poke in until I am satisfied with the states. This might work, but feels like a dirty hack.
Any suggestions are greatly appreciated.
As far as I understand, you can use a simple
try–finallyblock: