public class ThreadUtils
extends java.lang.Object
java.lang.Thread
and java.lang.ThreadGroup
.
#ThreadSafe#
Thread
,
ThreadGroup
Modifier and Type | Class and Description |
---|---|
static class |
ThreadUtils.NamePredicate
Deprecated.
Use a
Predicate . |
static interface |
ThreadUtils.ThreadGroupPredicate
Deprecated.
Use a
Predicate . |
static class |
ThreadUtils.ThreadIdPredicate
Deprecated.
Use a
Predicate . |
static interface |
ThreadUtils.ThreadPredicate
Deprecated.
Use a
Predicate . |
Modifier and Type | Field and Description |
---|---|
static org.apache.commons.lang3.ThreadUtils.AlwaysTruePredicate |
ALWAYS_TRUE_PREDICATE
Deprecated.
Use a
Predicate . |
Constructor and Description |
---|
ThreadUtils()
ThreadUtils instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.Thread |
findThreadById(long threadId)
Finds the active thread with the specified id.
|
static java.lang.Thread |
findThreadById(long threadId,
java.lang.String threadGroupName)
Finds the active thread with the specified id if it belongs to a thread group with the specified group name.
|
static java.lang.Thread |
findThreadById(long threadId,
java.lang.ThreadGroup threadGroup)
Finds the active thread with the specified id if it belongs to the specified thread group.
|
static java.util.Collection<java.lang.ThreadGroup> |
findThreadGroups(java.util.function.Predicate<java.lang.ThreadGroup> predicate)
Finds all active thread groups which match the given predicate.
|
static java.util.Collection<java.lang.ThreadGroup> |
findThreadGroups(java.lang.ThreadGroup threadGroup,
boolean recurse,
java.util.function.Predicate<java.lang.ThreadGroup> predicate)
Finds all active thread groups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
|
static java.util.Collection<java.lang.ThreadGroup> |
findThreadGroups(java.lang.ThreadGroup threadGroup,
boolean recurse,
ThreadUtils.ThreadGroupPredicate predicate)
Deprecated.
|
static java.util.Collection<java.lang.ThreadGroup> |
findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate)
Deprecated.
|
static java.util.Collection<java.lang.ThreadGroup> |
findThreadGroupsByName(java.lang.String threadGroupName)
Finds active thread groups with the specified group name.
|
static java.util.Collection<java.lang.Thread> |
findThreads(java.util.function.Predicate<java.lang.Thread> predicate)
Finds all active threads which match the given predicate.
|
static java.util.Collection<java.lang.Thread> |
findThreads(java.lang.ThreadGroup threadGroup,
boolean recurse,
java.util.function.Predicate<java.lang.Thread> predicate)
Finds all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
|
static java.util.Collection<java.lang.Thread> |
findThreads(java.lang.ThreadGroup threadGroup,
boolean recurse,
ThreadUtils.ThreadPredicate predicate)
Deprecated.
|
static java.util.Collection<java.lang.Thread> |
findThreads(ThreadUtils.ThreadPredicate predicate)
Deprecated.
|
static java.util.Collection<java.lang.Thread> |
findThreadsByName(java.lang.String threadName)
Finds active threads with the specified name.
|
static java.util.Collection<java.lang.Thread> |
findThreadsByName(java.lang.String threadName,
java.lang.String threadGroupName)
Finds active threads with the specified name if they belong to a thread group with the specified group name.
|
static java.util.Collection<java.lang.Thread> |
findThreadsByName(java.lang.String threadName,
java.lang.ThreadGroup threadGroup)
Finds active threads with the specified name if they belong to a specified thread group.
|
static java.util.Collection<java.lang.ThreadGroup> |
getAllThreadGroups()
Gets all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
|
static java.util.Collection<java.lang.Thread> |
getAllThreads()
Gets all active threads (A thread is active if it has been started and has not yet died).
|
static java.lang.ThreadGroup |
getSystemThreadGroup()
Gets the system thread group (sometimes also referred as "root thread group").
|
static void |
join(java.lang.Thread thread,
java.time.Duration duration)
Waits for the given thread to die for the given duration.
|
static void |
sleep(java.time.Duration duration)
Sleeps the current thread for the given duration.
|
static void |
sleepQuietly(java.time.Duration duration)
Sleeps for the given duration while ignoring
InterruptedException . |
@Deprecated public static final org.apache.commons.lang3.ThreadUtils.AlwaysTruePredicate ALWAYS_TRUE_PREDICATE
Predicate
.public ThreadUtils()
ThreadUtils.getAllThreads()
This constructor is public to permit tools that require a JavaBean instance to operate.
public static java.lang.Thread findThreadById(long threadId)
threadId
- The thread idnull
if no such thread existsjava.lang.IllegalArgumentException
- if the specified id is zero or negativejava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.lang.Thread findThreadById(long threadId, java.lang.String threadGroupName)
threadId
- The thread idthreadGroupName
- The thread group namenull
is returned if no such thread existsjava.lang.NullPointerException
- if the group name is nulljava.lang.IllegalArgumentException
- if the specified id is zero or negativejava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.lang.Thread findThreadById(long threadId, java.lang.ThreadGroup threadGroup)
threadId
- The thread idthreadGroup
- The thread groupnull
is returned if no such thread existsjava.lang.NullPointerException
- if threadGroup == null
java.lang.IllegalArgumentException
- if the specified id is zero or negativejava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.ThreadGroup> findThreadGroups(java.util.function.Predicate<java.lang.ThreadGroup> predicate)
predicate
- the predicateCollection
of active thread groups matching the given predicatejava.lang.NullPointerException
- if the predicate is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.ThreadGroup> findThreadGroups(java.lang.ThreadGroup threadGroup, boolean recurse, java.util.function.Predicate<java.lang.ThreadGroup> predicate)
threadGroup
- the thread grouprecurse
- if true
then evaluate the predicate recursively on all thread groups in all subgroups of the given grouppredicate
- the predicateCollection
of active thread groups which match the given predicate and which is a subgroup of the given thread groupjava.lang.NullPointerException
- if the given group or predicate is nulljava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static java.util.Collection<java.lang.ThreadGroup> findThreadGroups(java.lang.ThreadGroup threadGroup, boolean recurse, ThreadUtils.ThreadGroupPredicate predicate)
findThreadGroups(ThreadGroup, boolean, Predicate)
.threadGroup
- the thread grouprecurse
- if true
then evaluate the predicate recursively on all thread groups in all subgroups of the given grouppredicate
- the predicateCollection
of active thread groups which match the given predicate and which is a subgroup of the given thread groupjava.lang.NullPointerException
- if the given group or predicate is nulljava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static java.util.Collection<java.lang.ThreadGroup> findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate)
findThreadGroups(Predicate)
.predicate
- the predicateCollection
of active thread groups matching the given predicatejava.lang.NullPointerException
- if the predicate is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.ThreadGroup> findThreadGroupsByName(java.lang.String threadGroupName)
threadGroupName
- The thread group namejava.lang.NullPointerException
- if group name is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.Thread> findThreads(java.util.function.Predicate<java.lang.Thread> predicate)
predicate
- the predicateCollection
of active threads matching the given predicatejava.lang.NullPointerException
- if the predicate is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.Thread> findThreads(java.lang.ThreadGroup threadGroup, boolean recurse, java.util.function.Predicate<java.lang.Thread> predicate)
threadGroup
- the thread grouprecurse
- if true
then evaluate the predicate recursively on all threads in all subgroups of the given grouppredicate
- the predicateCollection
of active threads which match the given predicate and which belongs to the given thread groupjava.lang.NullPointerException
- if the given group or predicate is nulljava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static java.util.Collection<java.lang.Thread> findThreads(java.lang.ThreadGroup threadGroup, boolean recurse, ThreadUtils.ThreadPredicate predicate)
findThreads(ThreadGroup, boolean, Predicate)
.threadGroup
- the thread grouprecurse
- if true
then evaluate the predicate recursively on all threads in all subgroups of the given grouppredicate
- the predicateCollection
of active threads which match the given predicate and which belongs to the given thread groupjava.lang.NullPointerException
- if the given group or predicate is nulljava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static java.util.Collection<java.lang.Thread> findThreads(ThreadUtils.ThreadPredicate predicate)
findThreads(Predicate)
.predicate
- the predicateCollection
of active threads matching the given predicatejava.lang.NullPointerException
- if the predicate is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.Thread> findThreadsByName(java.lang.String threadName)
threadName
- The thread namejava.lang.NullPointerException
- if the specified name is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.Thread> findThreadsByName(java.lang.String threadName, java.lang.String threadGroupName)
threadName
- The thread namethreadGroupName
- The thread group namejava.lang.NullPointerException
- if the specified thread name or group name is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.Thread> findThreadsByName(java.lang.String threadName, java.lang.ThreadGroup threadGroup)
threadName
- The thread namethreadGroup
- The thread groupjava.lang.NullPointerException
- if the specified thread name or group is nulljava.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.ThreadGroup> getAllThreadGroups()
java.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.util.Collection<java.lang.Thread> getAllThreads()
java.lang.SecurityException
- if the current thread cannot access the system thread groupjava.lang.SecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static java.lang.ThreadGroup getSystemThreadGroup()
This method returns null if this thread has died (been stopped).
java.lang.SecurityException
- if the current thread cannot modify thread groups from this thread's thread group up to the
system thread grouppublic static void join(java.lang.Thread thread, java.time.Duration duration) throws java.lang.InterruptedException
Thread.join(long, int)
.thread
- The thread to join.duration
- How long to wait.java.lang.InterruptedException
- if any thread has interrupted the current thread.Thread.join(long, int)
public static void sleep(java.time.Duration duration) throws java.lang.InterruptedException
Thread.sleep(long, int)
.duration
- How long to sleep.java.lang.InterruptedException
- if any thread has interrupted the current thread.Thread.sleep(long, int)
public static void sleepQuietly(java.time.Duration duration)
InterruptedException
.
The sleep duration may be shorter than duration if we catch a InterruptedException
.
duration
- the length of time to sleep.Copyright © 2010 - 2023 Adobe. All Rights Reserved