Package org.apache.commons.lang3
Class ThreadUtils
java.lang.Object
org.apache.commons.lang3.ThreadUtils
- Since:
- 3.5
- See Also:
- 
Nested Class SummaryNested Classes
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final org.apache.commons.lang3.ThreadUtils.AlwaysTruePredicateDeprecated.Use aPredicate.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic ThreadfindThreadById(long threadId) Finds the active thread with the specified id.static ThreadfindThreadById(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 ThreadfindThreadById(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 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(Predicate<ThreadGroup> predicate) Finds all active thread groups which match the given predicate.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(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(Predicate<Thread> predicate) Finds all active threads which match the given predicate.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>Gets all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).static Collection<Thread>Gets all active threads (A thread is active if it has been started and has not yet died).static ThreadGroupGets the system thread group (sometimes also referred as "root thread group").static voidWaits for the given thread to die for the given duration.static voidSleeps the current thread for the given duration.static voidsleepQuietly(Duration duration) Sleeps for the given duration while ignoringInterruptedException.
- 
Field Details- 
ALWAYS_TRUE_PREDICATE@Deprecated public static final org.apache.commons.lang3.ThreadUtils.AlwaysTruePredicate ALWAYS_TRUE_PREDICATEDeprecated.Use aPredicate.Predicate which always returns true.
 
- 
- 
Constructor Details- 
ThreadUtilsDeprecated.TODO Make private in 4.0.ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used asThreadUtils.getAllThreads()This constructor is public to permit tools that require a JavaBean instance to operate. 
 
- 
- 
Method Details- 
findThreadByIdFinds the active thread with the specified id.- Parameters:
- threadId- The thread id
- Returns:
- The thread with the specified id or nullif no such thread exists
- Throws:
- IllegalArgumentException- if the specified id is zero or negative
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadByIdFinds the active thread with the specified id if it belongs to a thread group with the specified group name.- Parameters:
- threadId- The thread id
- threadGroupName- The thread group name
- Returns:
- The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
 nullis returned if no such thread exists
- Throws:
- NullPointerException- if the group name is null
- IllegalArgumentException- if the specified id is zero or negative
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadByIdFinds the active thread with the specified id if it belongs to the specified thread group.- Parameters:
- threadId- The thread id
- threadGroup- The thread group
- Returns:
- The thread which belongs to a specified thread group and the thread's id match the specified id.
 nullis returned if no such thread exists
- Throws:
- NullPointerException- if- threadGroup == null
- IllegalArgumentException- if the specified id is zero or negative
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadGroupsFinds all active thread groups which match the given predicate.- Parameters:
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active thread groups matching the given predicate
- Throws:
- NullPointerException- if the predicate is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
- Since:
- 3.13.0
 
- 
findThreadGroupspublic 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).- Parameters:
- threadGroup- the thread group
- recurse- if- truethen evaluate the predicate recursively on all thread groups in all subgroups of the given group
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active thread groups which match the given predicate and which is a subgroup of the given thread group
- Throws:
- NullPointerException- if the given group or predicate is null
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
- Since:
- 3.13.0
 
- 
findThreadGroups@Deprecated public static Collection<ThreadGroup> findThreadGroups(ThreadGroup threadGroup, boolean recurse, ThreadUtils.ThreadGroupPredicate predicate) Deprecated.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).- Parameters:
- threadGroup- the thread group
- recurse- if- truethen evaluate the predicate recursively on all thread groups in all subgroups of the given group
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active thread groups which match the given predicate and which is a subgroup of the given thread group
- Throws:
- NullPointerException- if the given group or predicate is null
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadGroups@Deprecated public static Collection<ThreadGroup> findThreadGroups(ThreadUtils.ThreadGroupPredicate predicate) Deprecated.Finds all active thread groups which match the given predicate.- Parameters:
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active thread groups matching the given predicate
- Throws:
- NullPointerException- if the predicate is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadGroupsByNameFinds active thread groups with the specified group name.- Parameters:
- threadGroupName- The thread group name
- Returns:
- the thread groups with the specified group name or an empty collection if no such thread group exists. The collection returned is always unmodifiable.
- Throws:
- NullPointerException- if group name is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadsFinds all active threads which match the given predicate.- Parameters:
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active threads matching the given predicate
- Throws:
- NullPointerException- if the predicate is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
- Since:
- 3.13.0
 
- 
findThreadspublic 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).- Parameters:
- threadGroup- the thread group
- recurse- if- truethen evaluate the predicate recursively on all threads in all subgroups of the given group
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active threads which match the given predicate and which belongs to the given thread group
- Throws:
- NullPointerException- if the given group or predicate is null
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
- Since:
- 3.13.0
 
- 
findThreads@Deprecated public static Collection<Thread> findThreads(ThreadGroup threadGroup, boolean recurse, ThreadUtils.ThreadPredicate predicate) Deprecated.Finds all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).- Parameters:
- threadGroup- the thread group
- recurse- if- truethen evaluate the predicate recursively on all threads in all subgroups of the given group
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active threads which match the given predicate and which belongs to the given thread group
- Throws:
- NullPointerException- if the given group or predicate is null
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadsDeprecated.Finds all active threads which match the given predicate.- Parameters:
- predicate- the predicate
- Returns:
- An unmodifiable Collectionof active threads matching the given predicate
- Throws:
- NullPointerException- if the predicate is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadsByNameFinds active threads with the specified name.- Parameters:
- threadName- The thread name
- Returns:
- The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
- Throws:
- NullPointerException- if the specified name is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadsByNameFinds active threads with the specified name if they belong to a thread group with the specified group name.- Parameters:
- threadName- The thread name
- threadGroupName- The thread group name
- Returns:
- The threads which belongs to a thread group with the specified group name and the thread's name match the specified name, An empty collection is returned if no such thread exists. The collection returned is always unmodifiable.
- Throws:
- NullPointerException- if the specified thread name or group name is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
findThreadsByNameFinds active threads with the specified name if they belong to a specified thread group.- Parameters:
- threadName- The thread name
- threadGroup- The thread group
- Returns:
- The threads which belongs to a thread group and the thread's name match the specified name, An empty collection is returned if no such thread exists. The collection returned is always unmodifiable.
- Throws:
- NullPointerException- if the specified thread name or group is null
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
getAllThreadGroupsGets all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).- Returns:
- all thread groups excluding the system thread group. The collection returned is always unmodifiable.
- Throws:
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
getAllThreadsGets all active threads (A thread is active if it has been started and has not yet died).- Returns:
- all active threads. The collection returned is always unmodifiable.
- Throws:
- SecurityException- if the current thread cannot access the system thread group
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
getSystemThreadGroupGets the system thread group (sometimes also referred as "root thread group").This method returns null if this thread has died (been stopped). - Returns:
- the system thread group
- Throws:
- SecurityException- if the current thread cannot modify thread groups from this thread's thread group up to the system thread group
 
- 
joinWaits for the given thread to die for the given duration. Implemented usingThread.join(long, int).- Parameters:
- thread- The thread to join.
- duration- How long to wait.
- Throws:
- InterruptedException- if any thread has interrupted the current thread.
- Since:
- 3.12.0
- See Also:
 
- 
sleepSleeps the current thread for the given duration. Implemented usingThread.sleep(long, int).- Parameters:
- duration- How long to sleep.
- Throws:
- InterruptedException- if any thread has interrupted the current thread.
- Since:
- 3.12.0
- See Also:
 
- 
sleepQuietlySleeps for the given duration while ignoringInterruptedException.The sleep duration may be shorter than duration if we catch a InterruptedException.- Parameters:
- duration- the length of time to sleep.
- Since:
- 3.13.0
 
 
- 
Predicate.