public final class XThreads extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
defaultHandleUncaughtThrowable(Thread t,
Throwable e)
A copy of the JDK's default behavior for handling ultimately uncaught exceptions, as implemented in
the last fallback case of
ThreadGroup.uncaughtException(Thread, Throwable). |
static void |
executeDelayed(long millis,
Runnable action) |
static void |
executeSynchronized(Runnable logic) |
static <T> T |
executeSynchronized(Supplier<T> logic)
Very simple and naive way of handling an application's concurrency globally:
Every logic that has concurrent modifications and/or race conditions in it has to be executed via this mechanism, making an application's concurrent parts effectively single-threaded. While this is not foolproof (one critical block of logic spread over multiple calls can still create inconsistent state, waiting threads are selected randomly, etc.) and definitely not suitable for complex applications, it can be a conveniently simple, working way to make concurrency-wise simple applications sufficiently concurrency-safe. |
static String |
getCurrentMethodName() |
static String |
getMethodNameForDeclaringClass(Class<?> declaringClass) |
static String |
getMethodNameForDeclaringClassName(String declaringClassName) |
static String |
getSourcePosition() |
static StackTraceElement |
getStackTraceElement() |
static StackTraceElement |
getStackTraceElement(Integer index) |
static StackTraceElement |
getStackTraceElementForDeclaringClass(Class<?> declaringClass) |
static StackTraceElement |
getStackTraceElementForDeclaringClassName(String declaringClassName) |
static void |
sleep(long millis)
Causes the current thread to sleep the specified amount of milliseconds by calling
Thread.sleep(long). |
static void |
sleep(long millis,
int nanos)
Causes the current thread to sleep the specified amount of milliseconds by calling
Thread.sleep(long, int). |
static Thread |
start(Runnable runnable) |
static <T extends Thread> |
start(T thread) |
public static <T> T executeSynchronized(Supplier<T> logic)
T - the return value typelogic - the logic to executepublic static void executeSynchronized(Runnable logic)
logic - the logic to executeexecuteSynchronized(Supplier)public static final <T extends Thread> T start(T thread)
public static final void sleep(long millis)
Thread.sleep(long).
Should an InterruptedException of Thread.sleep(long) occur, this method restored the
interruption state by invoking Thread.interrupted() and reporting the InterruptedException
wrappedn in a RuntimeException.
The underlying rationale to this behavior is explained in an internal comment.
In short: generically interrupting a thread while ignoring the application/library state and logic is just
as naive and dangerous as Thread.stop() is. They realized it for that method. Interruption is nothing
different in this regard. Until that erratic and dangerous behavior is fixed, this method provides a
convenient encapsulation of handling the nonsense as well as possible.
millis - the length of time to sleep in millisecondsThread.sleep(long),
Thread.stop()public static final void sleep(long millis,
int nanos)
Thread.sleep(long, int).
Also see the explanations in sleep(long)
millis - the length of time to sleep in millisecondsnanos - 0-999999 additional nanoseconds to sleepThread.sleep(long),
Thread.stop()public static final void executeDelayed(long millis,
Runnable action)
public static final String getSourcePosition()
public static final StackTraceElement getStackTraceElement()
public static final StackTraceElement getStackTraceElement(Integer index)
public static StackTraceElement getStackTraceElementForDeclaringClass(Class<?> declaringClass)
public static StackTraceElement getStackTraceElementForDeclaringClassName(String declaringClassName)
public static String getMethodNameForDeclaringClass(Class<?> declaringClass)
public static String getMethodNameForDeclaringClassName(String declaringClassName)
public static String getCurrentMethodName()
public static void defaultHandleUncaughtThrowable(Thread t, Throwable e)
ThreadGroup.uncaughtException(Thread, Throwable).
Sadly, this copy is necessary as they one again failed to modularize their default logic adequately.
Such a method is strongly required if a custom default Thread.UncaughtExceptionHandler
only handles exceptions of some type and/or some threads and wants/needs to pass all others along to the
default logic.
As this is a copy of the JDK's logic, it suffers the typical problems of having to be updated manually in case the JDK's logic should ever change (which is not very probable in this case).
Copyright © 2022 MicroStream Software. All rights reserved.