java - Better way to handle Uncaught Exceptions in ForkJoinPool Tasks/action -
java - Better way to handle Uncaught Exceptions in ForkJoinPool Tasks/action -
what improve way handle exceptions(uncaught) while using forkjoinpool
submit tasks (recursiveaction
or recursivetask
)?
forkjoinpool accepts thread.uncaughtexceptionhandler
handle exceptions when workerthread terminates abruptly(which anyways not under our control) handler not used when forkjointask
throws exception. using standard submit
/invokeall
way in implementation.
here scenario:
i have thread running in infinite loop reading info 3rd party system. in thread submit tasks forkjoinpool
new thread() { public void run() { while (true) { forkjointask<void> uselessreturn = forkjoinpool.submit(recursiveactiontask); } } }
i using recursiveaction , in few scenarios recursivetask. these tasks submitted fjpool using submit()
method. want have generic exception handler similar uncaughtexceptionhandler
if task throws unchecked/uncaught exception can process exception , re-submit task if required. handling exception ensures queued tasks not cancelled if one/some of tasks throw exception.
invokeall()
method returns set of forkjointasks these tasks in recursive block (each task invokes compute()
method , may split farther [hypothetical scenario] )
class recursiveactiontask extends recursiveaction { public void compute() { if <task.size() <= acceptable_size) { processtask() // might throw checked/unchecked exception } else { recursiveactiontask[] splittasks = splittasks(tasks) recursiveactiontasks returnedtasks = invokeall(splittasks); // below code never executes invokeall submits tasks pool // , flow never comes code below. // looking handling (recusiveactiontask task : returnedtasks) { if (task.isdone()) { task.getexception() // handle exception } } } } }
i noticed when 3-4 tasks fail whole queue submission unit discarded. have set try/catch
around processtask don't like. looking more generic.
get()
method on futuretask more set flow sequential waits until task completes. i want know status of task if fails. don't care when completes (obviously doesn't want wait hr later) any ideas how handle exceptions in above scenario?
this show solved in akka:
/** * internal akka usage */ final class mailboxexecutiontask(mailbox: mailbox) extends forkjointask[unit] { final override def setrawresult(u: unit): unit = () final override def getrawresult(): unit = () final override def exec(): boolean = seek { mailbox.run; true } grab { case ⇒ val t = thread.currentthread t.getuncaughtexceptionhandler match { case null ⇒ case ⇒ some.uncaughtexception(t, anything) } throw } }
java multithreading java-7 java.util.concurrent fork-join
Comments
Post a Comment