Class XThreads


  • public final class XThreads
    extends Object
    • Constructor Detail

      • XThreads

        public XThreads()
    • Method Detail

      • executeSynchronized

        public 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.
        Type Parameters:
        T -
        Parameters:
        logic -
      • start

        public static final <T extends Thread> T start​(T thread)
      • sleep

        public static final void sleep​(long millis)
        Causes the current thread to sleep the specified amount of milliseconds by calling Thread.sleep(long). Should an InterruptedException of Thread.sleep(long) occur, this method restored the interruption state by invoking Thread.interrupted() and reporting the InterruptedExceptionwrappedn 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.

        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 calling Thread.sleep(long,int).

        Also see the explanations in sleep(long)

        Parameters:
        millis - the length of time to sleep in milliseconds
        nanos - 0-999999 additional nanoseconds to sleep
        See Also:
        Thread.sleep(long), Thread.stop()
      • executeDelayed

        public static final void executeDelayed​(long millis,
                                                Runnable action)
      • getSourcePosition

        public static final String getSourcePosition()
      • getStackTraceElement

        public static final StackTraceElement getStackTraceElement()
      • getStackTraceElementForDeclaringClass

        public static StackTraceElement getStackTraceElementForDeclaringClass​(Class<?> declaringClass)
      • getStackTraceElementForDeclaringClassName

        public static StackTraceElement getStackTraceElementForDeclaringClassName​(String declaringClassName)
      • getMethodNameForDeclaringClass

        public static String getMethodNameForDeclaringClass​(Class<?> declaringClass)
      • getMethodNameForDeclaringClassName

        public static String getMethodNameForDeclaringClassName​(String declaringClassName)
      • getCurrentMethodName

        public static String getCurrentMethodName()
      • defaultHandleUncaughtThrowable

        public 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).

        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.UncaughtExceptionHandleronly 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).

        Parameters:
        t - the thread that caused the Throwable e.
        e - the Throwable to be handled.