public class ThreadUtils extends Object
Helpers for java.lang.Thread
and java.lang.ThreadGroup
.
#ThreadSafe#
Thread
,
ThreadGroup
Modifier and Type | Class and Description |
---|---|
static class |
ThreadUtils.NamePredicate
A predicate implementation which matches a thread or threadgroup name.
|
static interface |
ThreadUtils.ThreadGroupPredicate
A predicate for selecting threadgroups.
|
static class |
ThreadUtils.ThreadIdPredicate
A predicate implementation which matches a thread id.
|
static interface |
ThreadUtils.ThreadPredicate
A predicate for selecting threads.
|
Modifier and Type | Field and Description |
---|---|
static org.apache.commons.lang3.ThreadUtils.AlwaysTruePredicate |
ALWAYS_TRUE_PREDICATE
Predicate which always returns true.
|
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(ThreadGroup group,
boolean recurse,
ThreadUtils.ThreadGroupPredicate predicate)
Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
|
static Collection<ThreadGroup> |
findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate)
Select all active threadgroups which match the given predicate.
|
static Collection<ThreadGroup> |
findThreadGroupsByName(String threadGroupName)
Finds active thread groups with the specified group name.
|
static Collection<Thread> |
findThreads(ThreadGroup group,
boolean recurse,
ThreadUtils.ThreadPredicate predicate)
Select 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(ThreadUtils.ThreadPredicate predicate)
Select all active threads which match the given predicate.
|
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.
|
public static final org.apache.commons.lang3.ThreadUtils.AlwaysTruePredicate ALWAYS_TRUE_PREDICATE
public ThreadUtils()
ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
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 existsIllegalArgumentException
- if the specified id is zero or negative or the 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 Thread findThreadById(long threadId, ThreadGroup threadGroup)
threadId
- The thread idthreadGroup
- The thread groupnull
is returned if no such thread existsIllegalArgumentException
- if the specified id is zero or negative or the 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> findThreadGroups(ThreadGroup group, boolean recurse, ThreadUtils.ThreadGroupPredicate predicate)
group
- the thread grouprecurse
- if true
then evaluate the predicate recursively on all threadgroups in all subgroups of the given grouppredicate
- the predicateCollection
of active threadgroups which match the given predicate and which is a subgroup of the given thread groupIllegalArgumentException
- 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 grouppublic static Collection<ThreadGroup> findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate)
predicate
- the predicateCollection
of active threadgroups matching the given predicateIllegalArgumentException
- 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 nameIllegalArgumentException
- 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(ThreadGroup group, boolean recurse, ThreadUtils.ThreadPredicate predicate)
group
- 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 groupIllegalArgumentException
- 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 grouppublic static Collection<Thread> findThreads(ThreadUtils.ThreadPredicate predicate)
predicate
- the predicateCollection
of active threads matching the given predicateIllegalArgumentException
- 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 nameIllegalArgumentException
- 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 nameIllegalArgumentException
- 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 groupIllegalArgumentException
- 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()
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)
Copyright © 2001–2021 The Apache Software Foundation. All rights reserved.