public class ThreadUtils extends 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 Thread |
findThreadById(long threadId)
Finds the active thread with the specified id.
|
static Thread |
findThreadById(long threadId,
String threadGroupName)
Finds the active thread with the specified id if it belongs to a thread group with the specified group name.
|
static Thread |
findThreadById(long threadId,
ThreadGroup threadGroup)
Finds the active thread with the specified id if it belongs to the specified thread group.
|
static Collection<ThreadGroup> |
findThreadGroups(Predicate<ThreadGroup> predicate)
Finds all active thread groups which match the given predicate.
|
static Collection<ThreadGroup> |
findThreadGroups(ThreadGroup threadGroup,
boolean recurse,
Predicate<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 Collection<ThreadGroup> |
findThreadGroups(ThreadGroup threadGroup,
boolean recurse,
ThreadUtils.ThreadGroupPredicate predicate)
Deprecated.
|
static Collection<ThreadGroup> |
findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate)
Deprecated.
|
static Collection<ThreadGroup> |
findThreadGroupsByName(String threadGroupName)
Finds active thread groups with the specified group name.
|
static Collection<Thread> |
findThreads(Predicate<Thread> predicate)
Finds all active threads which match the given predicate.
|
static Collection<Thread> |
findThreads(ThreadGroup threadGroup,
boolean recurse,
Predicate<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 Collection<Thread> |
findThreads(ThreadGroup threadGroup,
boolean recurse,
ThreadUtils.ThreadPredicate predicate)
Deprecated.
|
static Collection<Thread> |
findThreads(ThreadUtils.ThreadPredicate predicate)
Deprecated.
|
static Collection<Thread> |
findThreadsByName(String threadName)
Finds active threads with the specified name.
|
static Collection<Thread> |
findThreadsByName(String threadName,
String threadGroupName)
Finds active threads with the specified name if they belong to a thread group with the specified group name.
|
static Collection<Thread> |
findThreadsByName(String threadName,
ThreadGroup threadGroup)
Finds active threads with the specified name if they belong to a specified thread group.
|
static Collection<ThreadGroup> |
getAllThreadGroups()
Gets all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
|
static Collection<Thread> |
getAllThreads()
Gets all active threads (A thread is active if it has been started and has not yet died).
|
static ThreadGroup |
getSystemThreadGroup()
Gets the system thread group (sometimes also referred as "root thread group").
|
static void |
join(Thread thread,
Duration duration)
Waits for the given thread to die for the given duration.
|
static void |
sleep(Duration duration)
Sleeps the current thread for the given duration.
|
static void |
sleepQuietly(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 Thread findThreadById(long threadId)
threadId
- The thread idnull
if no such thread existsIllegalArgumentException
- if the specified id is zero or negativeSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Thread findThreadById(long threadId, String threadGroupName)
threadId
- The thread idthreadGroupName
- The thread group namenull
is returned if no such thread existsNullPointerException
- if the group name is nullIllegalArgumentException
- if the specified id is zero or negativeSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Thread findThreadById(long threadId, ThreadGroup threadGroup)
threadId
- The thread idthreadGroup
- The thread groupnull
is returned if no such thread existsNullPointerException
- if threadGroup == null
IllegalArgumentException
- if the specified id is zero or negativeSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<ThreadGroup> findThreadGroups(Predicate<ThreadGroup> predicate)
predicate
- the predicateCollection
of active thread groups matching the given predicateNullPointerException
- if the predicate is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<ThreadGroup> findThreadGroups(ThreadGroup threadGroup, boolean recurse, Predicate<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 groupNullPointerException
- if the given group or predicate is nullSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static Collection<ThreadGroup> findThreadGroups(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 groupNullPointerException
- if the given group or predicate is nullSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static Collection<ThreadGroup> findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate)
findThreadGroups(Predicate)
.predicate
- the predicateCollection
of active thread groups matching the given predicateNullPointerException
- if the predicate is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<ThreadGroup> findThreadGroupsByName(String threadGroupName)
threadGroupName
- The thread group nameNullPointerException
- if group name is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<Thread> findThreads(Predicate<Thread> predicate)
predicate
- the predicateCollection
of active threads matching the given predicateNullPointerException
- if the predicate is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<Thread> findThreads(ThreadGroup threadGroup, boolean recurse, Predicate<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 groupNullPointerException
- if the given group or predicate is nullSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static Collection<Thread> findThreads(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 groupNullPointerException
- if the given group or predicate is nullSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread group@Deprecated public static Collection<Thread> findThreads(ThreadUtils.ThreadPredicate predicate)
findThreads(Predicate)
.predicate
- the predicateCollection
of active threads matching the given predicateNullPointerException
- if the predicate is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<Thread> findThreadsByName(String threadName)
threadName
- The thread nameNullPointerException
- if the specified name is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<Thread> findThreadsByName(String threadName, String threadGroupName)
threadName
- The thread namethreadGroupName
- The thread group nameNullPointerException
- if the specified thread name or group name is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<Thread> findThreadsByName(String threadName, ThreadGroup threadGroup)
threadName
- The thread namethreadGroup
- The thread groupNullPointerException
- if the specified thread name or group is nullSecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<ThreadGroup> getAllThreadGroups()
SecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static Collection<Thread> getAllThreads()
SecurityException
- if the current thread cannot access the system thread groupSecurityException
- if the current thread cannot modify
thread groups from this thread's thread group up to the system thread grouppublic static ThreadGroup getSystemThreadGroup()
This method returns null if this thread has died (been stopped).
SecurityException
- if the current thread cannot modify thread groups from this thread's thread group up to the
system thread grouppublic static void join(Thread thread, Duration duration) throws InterruptedException
Thread.join(long, int)
.thread
- The thread to join.duration
- How long to wait.InterruptedException
- if any thread has interrupted the current thread.Thread.join(long, int)
public static void sleep(Duration duration) throws InterruptedException
Thread.sleep(long, int)
.duration
- How long to sleep.InterruptedException
- if any thread has interrupted the current thread.Thread.sleep(long, int)
public static void sleepQuietly(Duration duration)
InterruptedException
.
The sleep duration may be shorter than duration if we catch a InterruptedException
.
duration
- the length of time to sleep.Copyright © 2001–2023 The Apache Software Foundation. All rights reserved.