public final class VerboseRunnable extends Object implements Runnable
Runnable
, that logs all uncaught runtime exceptions.
You can use it with scheduled executor, for example:
Executors.newScheduledThreadPool(2).scheduleAtFixedRate( new VerboseRunnable(runnable, true), 1L, 1L, TimeUnit.SECONDS );
Now, every runtime exception that is not caught inside your
Runnable
will be reported to log (using Logger
).
Two-arguments constructor can be used when you need to instruct the class
about what to do with the exception: either swallow it or escalate.
Sometimes it's very important to swallow exceptions. Otherwise an entire
thread may get stuck (like in the example above).
Since version 0.7.16, besides just swallowing exceptions the class also clears the interrupted status. That means that the thread with the runnable can be interrupted but will never expose its interrupted status.
This class is thread-safe.
VerboseThreads
Constructor and Description |
---|
VerboseRunnable(Callable<?> callable)
Default constructor, doesn't swallow exceptions.
|
VerboseRunnable(Callable<?> callable,
boolean swallowexceptions)
Default constructor, doesn't swallow exceptions.
|
VerboseRunnable(Callable<?> callable,
boolean swallowexceptions,
boolean fullstacktrace)
Default constructor.
|
VerboseRunnable(Runnable runnable)
Default constructor, doesn't swallow exceptions.
|
VerboseRunnable(Runnable runnable,
boolean swallowexceptions)
Default constructor, with configurable behavior for exceptions.
|
VerboseRunnable(Runnable runnable,
boolean swallowexceptions,
boolean fullstacktrace)
Default constructor, with fully configurable behavior.
|
public VerboseRunnable(@NotNull Runnable runnable)
runnable
- Runnable to wrappublic VerboseRunnable(@NotNull Callable<?> callable)
callable
- Callable to wrappublic VerboseRunnable(@NotNull Callable<?> callable, boolean swallowexceptions)
public VerboseRunnable(@NotNull Callable<?> callable, boolean swallowexceptions, boolean fullstacktrace)
callable
- Callable to wrapswallowexceptions
- Shall we swallow exceptions
(TRUE
) or re-throw
(FALSE
)? Exception swallowing means that run()
will never throw any exceptions (in any case all exceptions are logged
using Logger
.fullstacktrace
- Shall we report the entire
stacktrace of the exception
(TRUE
) or just its message in one line (FALSE
)public VerboseRunnable(@NotNull Runnable runnable, boolean swallowexceptions)
public VerboseRunnable(@NotNull Runnable runnable, boolean swallowexceptions, boolean fullstacktrace)
runnable
- Runnable to wrapswallowexceptions
- Shall we swallow exceptions
(TRUE
) or re-throw
(FALSE
)? Exception swallowing means that run()
will never throw any exceptions (in any case all exceptions are logged
using Logger
.fullstacktrace
- Shall we report the entire
stacktrace of the exception
(TRUE
) or just its message in one line (FALSE
)public void run()
We catch RuntimeException
and Error
here. All other
types of exceptions are "checked exceptions" and won't be thrown out
of Runnable#run()
method.
Copyright © 2012–2014 jcabi.com. All rights reserved.