Class Threads

java.lang.Object
com.globalmentor.java.Threads

public class Threads extends Object
Utility methods for threads.
Author:
Garret Wilson
  • Constructor Details

    • Threads

      public Threads()
  • Method Details

    • join

      public static void join(Thread thread)
      Joins a thread, ignoring any interruptions. If any interruptions occur, they will be ignored and joining will be attempted again.
      Parameters:
      thread - The thread to join.
    • call

      public static void call(Thread thread)
      Calls a thread serially by first starting the thread and waiting until the thread is finished.
      Parameters:
      thread - The thread to call.
      See Also:
    • call

      public static Thread call(ThreadGroup threadGroup, Runnable runnable)
      Creates a thread in the given thread group and calls the given runnable in the thread, waiting until the thread is finished. This version catches any uncaught Error or RuntimeException in the thread and rethrows it from this thread. If any other Throwable is thrown in the thread, this method throws an UndeclaredThrowableException with that throwable as the cause. This method delegates to call(ThreadGroup, Runnable, java.lang.Thread.UncaughtExceptionHandler)
      Parameters:
      threadGroup - The thread group in which the thread is to be run.
      runnable - The runnable interface to run in the thread.
      Returns:
      The thread that has been run in the thread group.
      Throws:
      UndeclaredThrowableException - if the thread throws any Throwable that is not an Error or a RuntimeException.
      See Also:
    • call

      public static Thread call(ThreadGroup threadGroup, Runnable runnable, Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
      Creates a thread in the given thread group and calls the given runnable in the thread, waiting until the thread is finished.
      Parameters:
      threadGroup - The thread group in which the thread is to be run.
      runnable - The runnable interface to run in the thread.
      uncaughtExceptionHandler - The exception handler for the thread, or null if no exception handler should be installed.
      Returns:
      The thread that has been run in the thread group.
      See Also:
    • getThreadGroup

      public static <TG extends ThreadGroup> TG getThreadGroup(Class<TG> threadGroupClass)
      Walks up the thread group chain of the current thread to find the thread group of the given type.
      Type Parameters:
      TG - The type of thread group to find.
      Parameters:
      threadGroupClass - The class of the type of thread group to find.
      Returns:
      The first thread group of the given type, or null if no thread group of the given type could be found.
      Throws:
      NullPointerException - if the given thread group class is null.
    • getThreadGroup

      public static <TG> TG getThreadGroup(Thread thread, Class<TG> threadGroupClass)
      Walks up the thread group chain of the given thread to find the thread group of the given type.
      Type Parameters:
      TG - The type of thread group to find.
      Parameters:
      thread - The thread at which the thread group search should begin.
      threadGroupClass - The class of the type of thread group to find.
      Returns:
      The first thread group of the given type, or null if no thread group of the given type could be found.
      Throws:
      NullPointerException - if the given thread and/or thread group class is null.
    • getThreadGroup

      public static <TG> TG getThreadGroup(ThreadGroup threadGroup, Class<TG> threadGroupClass)
      Walks up the thread group chain of the given thread group to find the thread group of the given type or that implements the given interface.
      Type Parameters:
      TG - The type of thread group to find.
      Parameters:
      threadGroup - The thread group at which the search should begin.
      threadGroupClass - The class of the type of thread group to find.
      Returns:
      The first thread group of the given type, or null if no thread group of the given type could be found.
      Throws:
      NullPointerException - if the given thread group and/or thread group class is null.
    • rethrow

      public static void rethrow(Throwable throwable) throws RuntimeException, Error, UndeclaredThrowableException
      Rethrows an exception. This is useful in cases where general Throwables are caught, some action is taken, and then those throwables need to be rethrown. Because a general throwable cannot be thrown without knowing if it needs to be declared, this method appropriately rethrows RuntimeExceptions and Errors, and wraps all other exceptions in an UndeclaredThrowableException.
      Parameters:
      throwable - The throwable to rethrow.
      Throws:
      NullPointerException - if the given throwable is null.
      RuntimeException
      Error
      UndeclaredThrowableException
    • getCallingClass

      @Deprecated public static Class<?> getCallingClass()
      Deprecated.
      Returns the class of the caller on the stack before the class of this method's caller.

      All callers from this class are ignored to allow for the various convenience forms of this method.

      Returns:
      the class of the last line of program execution before a method in the class of the caller to this method.
      Throws:
      IllegalStateException - if there was no caller in the stack before the caller to this method.
    • getCallingClass

      @Deprecated public static Class<?> getCallingClass(Class<?> ignoreClass)
      Deprecated.
      Returns the class of the caller on the stack before the class of this method's caller, ignoring any occurrences of the given class, if any.

      All callers from this class are ignored to allow for the various convenience forms of this method.

      Parameters:
      ignoreClass - The class to ignore in the stack, or null if the first caller before this method's caller should be returned and no other classes should be ignored.
      Returns:
      the class of the last line of program execution before a method in the class of the caller to this method, skipping the ignore class, if any.
      Throws:
      IllegalStateException - if no ignore class was provided and there was no caller in the stack before the caller to this method.
      IllegalArgumentException - if an ignore class was provided and there was no caller in the stack before the caller to this method that wasn't from the given class.
    • getCallingClassStackTraceElement

      @Deprecated public static StackTraceElement getCallingClassStackTraceElement()
      Deprecated.
      Returns a stack trace element describing the caller on the stack before the class of this method's caller.

      All callers from this class are ignored to allow for the various convenience forms of this method.

      Returns:
      A stack trace element representing the last line of program execution before a method in the class of the caller to this method.
      Throws:
      IllegalStateException - if there was no caller in the stack before the caller to this method.
    • getCallingClassStackTraceElement

      @Deprecated public static StackTraceElement getCallingClassStackTraceElement(Class<?> ignoreClass)
      Deprecated.
      Returns a stack trace element describing the caller on the stack before the class of this method's caller, ignoring any occurrences of the given class, if any.

      All callers from this class are ignored to allow for the various convenience forms of this method.

      Parameters:
      ignoreClass - The class to ignore in the stack, or null if the first caller before this method's caller should be returned and no other classes should be ignored.
      Returns:
      A stack trace element representing the last line of program execution before a method in the class of the caller to this method, skipping the ignore class, if any.
      Throws:
      IllegalStateException - if no ignore class was provided and there was no caller in the stack before the caller to this method.
      IllegalArgumentException - if an ignore class was provided and there was no caller in the stack before the caller to this method that wasn't from the given class.