exception in threads

Jackie
1 min readJun 1, 2021

--

when the forked out thread is running the task, if any exception happens, depends on the mechanism how the thread/threadpool is constructed, it could handle the exception differently.

for forkjoinpool, if the task is executed. when the task failed with exception, it will propagate the exception, till it’s either handled or if uncaught then captured by the `UncaughtExceptionHandler`. (this is called by JVM).

when the task is submitted though, the task is wrapped into AdaptedRunnableAction, which doesn't propagate the exception.

I think what JCP/JVM trying to do is, trust the developers know the difference between the call and handle the exception from the returned task themselves.

final int doExec() { int s; boolean completed; if ((s = status) >= 0) { try { completed = exec(); } catch (Throwable rex) { return setExceptionalCompletion(rex); } if (completed) s = setCompletion(NORMAL); } return s; }

which in turn calls the propagateException

private int setExceptionalCompletion(Throwable ex) { int s = recordExceptionalCompletion(ex); if ((s & DONE_MASK) == EXCEPTIONAL) internalPropagateException(ex); return s; }

Originally published at https://lwpro2.dev on June 1, 2021.

--

--

No responses yet