Class XThreads
public final class XThreads extends Object
-
Constructor Summary
Constructors Constructor Description XThreads()
-
Method Summary
Modifier and Type Method 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 ofThreadGroup.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 callingThread.sleep(long)
.static void
sleep(long millis, int nanos)
Causes the current thread to sleep the specified amount of milliseconds by callingThread.sleep(long,int)
.static Thread
start(Runnable runnable)
static <T extends Thread>
Tstart(T thread)
-
Constructor Details
-
XThreads
public XThreads()
-
-
Method Details
-
executeSynchronized
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.- Type Parameters:
T
-- Parameters:
logic
-
-
executeSynchronized
- See Also:
executeSynchronized(Runnable)
-
start
-
start
-
sleep
public static final void sleep(long millis)Causes the current thread to sleep the specified amount of milliseconds by callingThread.sleep(long)
. Should anInterruptedException
ofThread.sleep(long)
occur, this method restored the interruption state by invokingThread.interrupted()
and reporting theInterruptedException
wrappedn in aRuntimeException
.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 asThread.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.- Parameters:
millis
- the length of time to sleep in milliseconds- See Also:
Thread.sleep(long)
,Thread.stop()
-
sleep
public static final void sleep(long millis, int nanos)Causes the current thread to sleep the specified amount of milliseconds by callingThread.sleep(long,int)
.Also see the explanations in
sleep(long)
- Parameters:
millis
- the length of time to sleep in millisecondsnanos
-0-999999
additional nanoseconds to sleep- See Also:
Thread.sleep(long)
,Thread.stop()
-
executeDelayed
-
getSourcePosition
-
getStackTraceElement
-
getStackTraceElement
-
getStackTraceElementForDeclaringClass
-
getStackTraceElementForDeclaringClassName
public static StackTraceElement getStackTraceElementForDeclaringClassName(String declaringClassName) -
getMethodNameForDeclaringClass
-
getMethodNameForDeclaringClassName
-
getCurrentMethodName
-
defaultHandleUncaughtThrowable
A copy of the JDK's default behavior for handling ultimately uncaught exceptions, as implemented in the last fallback case ofThreadGroup.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).
-