Class Threads


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

      Constructors 
      Constructor Description
      Threads()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void call​(java.lang.Thread thread)
      Calls a thread serially by first starting the thread and waiting until the thread is finished.
      static java.lang.Thread call​(java.lang.ThreadGroup threadGroup, java.lang.Runnable runnable)
      Creates a thread in the given thread group and calls the given runnable in the thread, waiting until the thread is finished.
      static java.lang.Thread call​(java.lang.ThreadGroup threadGroup, java.lang.Runnable runnable, java.lang.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.
      static java.lang.Class<?> getCallingClass()
      Deprecated.
      static java.lang.Class<?> getCallingClass​(java.lang.Class<?> ignoreClass)
      Deprecated.
      static java.lang.StackTraceElement getCallingClassStackTraceElement()
      Deprecated.
      static java.lang.StackTraceElement getCallingClassStackTraceElement​(java.lang.Class<?> ignoreClass)
      Deprecated.
      static <TG extends java.lang.ThreadGroup>
      TG
      getThreadGroup​(java.lang.Class<TG> threadGroupClass)
      Walks up the thread group chain of the current thread to find the thread group of the given type.
      static <TG> TG getThreadGroup​(java.lang.ThreadGroup threadGroup, java.lang.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.
      static <TG> TG getThreadGroup​(java.lang.Thread thread, java.lang.Class<TG> threadGroupClass)
      Walks up the thread group chain of the given thread to find the thread group of the given type.
      static void join​(java.lang.Thread thread)
      Joins a thread, ignoring any interruptions.
      static void rethrow​(java.lang.Throwable throwable)
      Rethrows an exception.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Threads

        public Threads()
    • Method Detail

      • join

        public static void join​(java.lang.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​(java.lang.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:
        Thread.start(), join(Thread)
      • call

        public static java.lang.Thread call​(java.lang.ThreadGroup threadGroup,
                                            java.lang.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:
        java.lang.reflect.UndeclaredThrowableException - if the thread throws any Throwable that is not an Error or a RuntimeException.
        See Also:
        call(Thread)
      • call

        public static java.lang.Thread call​(java.lang.ThreadGroup threadGroup,
                                            java.lang.Runnable runnable,
                                            java.lang.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:
        call(Thread)
      • getThreadGroup

        public static <TG extends java.lang.ThreadGroup> TG getThreadGroup​(java.lang.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:
        java.lang.NullPointerException - if the given thread group class is null.
      • getThreadGroup

        public static <TG> TG getThreadGroup​(java.lang.Thread thread,
                                             java.lang.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:
        java.lang.NullPointerException - if the given thread and/or thread group class is null.
      • getThreadGroup

        public static <TG> TG getThreadGroup​(java.lang.ThreadGroup threadGroup,
                                             java.lang.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:
        java.lang.NullPointerException - if the given thread group and/or thread group class is null.
      • rethrow

        public static void rethrow​(java.lang.Throwable throwable)
                            throws java.lang.RuntimeException,
                                   java.lang.Error,
                                   java.lang.reflect.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:
        java.lang.NullPointerException - if the given throwable is null.
        java.lang.RuntimeException
        java.lang.Error
        java.lang.reflect.UndeclaredThrowableException
      • getCallingClass

        @Deprecated
        public static java.lang.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:
        java.lang.IllegalStateException - if there was no caller in the stack before the caller to this method.
      • getCallingClass

        @Deprecated
        public static java.lang.Class<?> getCallingClass​(java.lang.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:
        java.lang.IllegalStateException - if no ignore class was provided and there was no caller in the stack before the caller to this method.
        java.lang.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 java.lang.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:
        java.lang.IllegalStateException - if there was no caller in the stack before the caller to this method.
      • getCallingClassStackTraceElement

        @Deprecated
        public static java.lang.StackTraceElement getCallingClassStackTraceElement​(java.lang.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:
        java.lang.IllegalStateException - if no ignore class was provided and there was no caller in the stack before the caller to this method.
        java.lang.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.