Package com.globalmentor.java
Class Threads
java.lang.Object
com.globalmentor.java.Threads
Utility methods for threads.
- Author:
- Garret Wilson
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Calls a thread serially by first starting the thread and waiting until the thread is finished.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.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.static Class<?>
Deprecated.static Class<?>
getCallingClass
(Class<?> ignoreClass) Deprecated.static StackTraceElement
Deprecated.static StackTraceElement
getCallingClassStackTraceElement
(Class<?> ignoreClass) Deprecated.static <TG extends ThreadGroup>
TGgetThreadGroup
(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
(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.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.static void
Joins a thread, ignoring any interruptions.static void
Rethrows an exception.
-
Constructor Details
-
Threads
public Threads()
-
-
Method Details
-
join
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
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
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 uncaughtError
orRuntimeException
in the thread and rethrows it from this thread. If any otherThrowable
is thrown in the thread, this method throws anUndeclaredThrowableException
with that throwable as the cause. This method delegates tocall(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 anyThrowable
that is not anError
or aRuntimeException
.- 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, ornull
if no exception handler should be installed.- Returns:
- The thread that has been run in the thread group.
- See Also:
-
getThreadGroup
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 isnull
.
-
getThreadGroup
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 isnull
.
-
getThreadGroup
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 isnull
.
-
rethrow
public static void rethrow(Throwable throwable) throws RuntimeException, Error, UndeclaredThrowableException Rethrows an exception. This is useful in cases where generalThrowable
s 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 rethrowsRuntimeException
s andError
s, and wraps all other exceptions in anUndeclaredThrowableException
.- Parameters:
throwable
- The throwable to rethrow.- Throws:
NullPointerException
- if the given throwable isnull
.RuntimeException
Error
UndeclaredThrowableException
-
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.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, ornull
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.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.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, ornull
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.
-