How to check if Java thread terminated with exception?

Given a thread that has completed some work (its status is Thread.State.TERMINATED), is there a way to figure out if Thread.run () / Runnable.run () completed the thread correctly or dropped the demand?

Afaik, the stream enters the TERMINATED state both when it exits the normal state and when metadata is thrown.

Someone suggested using UncaughtExceptionHandler. Given that a thread can have only one UncaughtExceptionHandler (except for the default value for all threads) and that the thread code can change the provided one, is it useful to use them?

+4
source share
4 answers

, , Callable, catch try {} Exception Future,

+1

I don’t know if there is a way to do this directly, but you can surround all your code in the run method with the try / catch statement. If you create a boolean and set it to true when it completes with an exception, you can check the state and if the boolean is true, it ends with an exception and if the boolean is false, then it closes correctly. There is probably a better way, but this should work.

0
source

Source: https://habr.com/ru/post/1568907/


All Articles