Package one.microstream.concurrency
Class XThreads
java.lang.Object
one.microstream.concurrency.XThreads
public final class XThreads extends Object
-
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)
-
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
- the return value type- Parameters:
logic
- the logic to execute- Returns:
- the supplier's value
-
executeSynchronized
- Parameters:
logic
- the logic to execute- See Also:
executeSynchronized(Supplier)
-
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
wrapped 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.- 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)
.Such a method is strongly required if a custom default
Thread.UncaughtExceptionHandler
only handles exceptions to 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).
-