You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by salyh <gi...@git.apache.org> on 2015/05/04 00:21:08 UTC
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
GitHub user salyh opened a pull request:
https://github.com/apache/commons-lang/pull/78
Add ThreadUtils (2nd PR)
Add org.apache.commons.lang3.ThreadUtils which provide helper methods related to java.lang.Thread (2nd PR)
This PR replaces https://github.com/apache/commons-lang/pull/61
ThreadUtils class exposes the following static methods:
findThreadById(long)
findThreadById(long, String)
findThreadById(long, ThreadGroup)
findThreadGroups(ThreadGroup, boolean, ThreadGroupPredicate)
findThreadGroups(ThreadGroupPredicate)
findThreadGroupsByName(String)
findThreads(ThreadGroup, boolean, ThreadPredicate)
findThreads(ThreadPredicate)
findThreadsByName(String)
findThreadsByName(String, String)
findThreadsByName(String, ThreadGroup)
getAllThreadGroups()
getAllThreads()
getSystemThreadGroup()
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/salyh/commons-lang threadutilsmaster
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/commons-lang/pull/78.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #78
----
commit 6f653f6bb82757963a87d725808c080109e7e3e8
Author: Hendrik Saly <he...@gmail.com>
Date: 2015-05-03T22:16:31Z
Add ThreadUtils
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by CodingFabian <gi...@git.apache.org>.
Github user CodingFabian commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29569586
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ NamePredicate threadNamePredicate = null;
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, threadNamePredicate==null?(threadNamePredicate=new NamePredicate(threadName)):threadNamePredicate));
--- End diff --
why not move the init of the threadName predicate to 130? is it lazy in case there are no results?
I personally find it hard to read, maybe assigning findThreadGroups() to a local variable and checking it is more readable and still efficient?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by peteryhwong <gi...@git.apache.org>.
Github user peteryhwong commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29562502
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,459 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, new NamePredicate(threadName)));
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Return active thread groups with the specified group name.
+ *
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
+ return findThreadGroups(new NamePredicate(threadGroupName));
+ }
+
+ /**
+ * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
+ *
+ * @return 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
+ *
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> getAllThreadGroups() {
+ return findThreadGroups(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return the system thread group (sometimes also referred as "root thread group").
+ *
+ * @return 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
+ */
+ public static ThreadGroup getSystemThreadGroup() {
+ ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+ while(threadGroup.getParent() != null) {
+ threadGroup = threadGroup.getParent();
+ }
+ return threadGroup;
+ }
+
+ /**
+ * Return all active threads (A thread is active if it has been started and has not yet died).
+ *
+ * @return all active threads. The collection returned is always unmodifiable.
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> getAllThreads() {
+ return findThreads(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return active threads with the specified name.
+ *
+ * @param threadName The thread name
+ * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
+ * @throws IllegalArgumentException if the specified name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName) {
+ return findThreads(new NamePredicate(threadName));
+ }
+
+ /**
+ * Return the active thread with the specified id.
+ *
+ * @param threadId The thread id
+ * @return The thread with the specified id or {@code null} if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId) {
+ final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
+
+ if(!result.iterator().hasNext()) {
+ return null;
+ } else {
+ return result.iterator().next();
+ }
+
+ }
+
+ /**
+ * <p>
+ * ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
+ * {@code ThreadUtils.getAllThreads()}
+ * </p>
+ * <p>
+ * This constructor is public to permit tools that require a JavaBean instance to operate.
+ * </p>
+ */
+ public ThreadUtils() {
+ super();
+ }
+
+ /**
+ * A predicate for selecting threads.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{
+
+ /**
+ * Evaluates this predicate on the given thread.
+ * @param thread the thread
+ * @return {@code true} if the thread matches the predicate, otherwise {@code false}
+ */
+ boolean test(Thread thread);
+ }
+
+ /**
+ * A predicate for selecting threadgroups.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{
+
+ /**
+ * Evaluates this predicate on the given threadgroup.
+ * @param threadGroup the threadgroup
+ * @return {@code true} if the threadGroup matches the predicate, otherwise {@code false}
+ */
+ boolean test(ThreadGroup threadGroup);
+ }
+
+ /**
+ * Predicate which always returns true.
+ */
+ public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
+
+ /**
+ * A predicate implementation which always returns true.
+ */
+ private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{
+
+ private AlwaysTruePredicate() {
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final ThreadGroup threadGroup) {
+ return true;
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final Thread thread) {
+ return true;
+ }
+
+ }
+
+ /**
+ * A predicate implementation which matches a thread or threadgroup name.
+ */
+ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate {
+
+ private final String name;
+
+ /**
+ * Predicate constructor
+ *
+ * @param name thread or threadgroup name
+ * @throws IllegalArgumentException if the name is {@code null}
+ */
+ public NamePredicate(final String name) {
+ super();
+ if (name == null) {
+ throw new IllegalArgumentException("The name must not be null");
+ }
+ this.name = name;
+ }
+
+ @Override
+ public boolean test(final ThreadGroup threadGroup) {
+ return threadGroup != null && threadGroup.getName().equals(name);
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getName().equals(name);
+ }
+ }
+
+ /**
+ * A predicate implementation which matches a thread id.
+ */
+ public static class ThreadIdPredicate implements ThreadPredicate {
+
+ private final long threadId;
+
+ /**
+ * Predicate constructor
+ *
+ * @param threadId the threadId to match
+ * @throws IllegalArgumentException if the threadId is zero or negative
+ */
+ public ThreadIdPredicate(final long threadId) {
+ super();
+ if (threadId <= 0) {
+ throw new IllegalArgumentException("The thread id must be greater than zero");
+ }
+ this.threadId = threadId;
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getId() == threadId;
+ }
+ }
+
+ /**
+ * Select all active threads which match the given predicate.
+ *
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads matching the given predicate
+ *
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreads(final ThreadPredicate predicate){
+ return findThreads(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate.
+ *
+ * @param predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){
+ return findThreadGroups(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) {
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ final List<Thread> result = new ArrayList<Thread>();
--- End diff --
If result is defined after the do while loop, it can be initialised with count as the initial capacity, thereby avoid the potential additional array allocation/copy when invoking add().
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/commons-lang/pull/78
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by peteryhwong <gi...@git.apache.org>.
Github user peteryhwong commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29562466
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,459 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, new NamePredicate(threadName)));
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Return active thread groups with the specified group name.
+ *
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
+ return findThreadGroups(new NamePredicate(threadGroupName));
+ }
+
+ /**
+ * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
+ *
+ * @return 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
+ *
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> getAllThreadGroups() {
+ return findThreadGroups(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return the system thread group (sometimes also referred as "root thread group").
+ *
+ * @return 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
+ */
+ public static ThreadGroup getSystemThreadGroup() {
+ ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+ while(threadGroup.getParent() != null) {
+ threadGroup = threadGroup.getParent();
+ }
+ return threadGroup;
+ }
+
+ /**
+ * Return all active threads (A thread is active if it has been started and has not yet died).
+ *
+ * @return all active threads. The collection returned is always unmodifiable.
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> getAllThreads() {
+ return findThreads(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return active threads with the specified name.
+ *
+ * @param threadName The thread name
+ * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
+ * @throws IllegalArgumentException if the specified name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName) {
+ return findThreads(new NamePredicate(threadName));
+ }
+
+ /**
+ * Return the active thread with the specified id.
+ *
+ * @param threadId The thread id
+ * @return The thread with the specified id or {@code null} if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId) {
+ final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
+
+ if(!result.iterator().hasNext()) {
--- End diff --
result.isEmpty() can be used instead to avoid creating an iterator instance.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by britter <gi...@git.apache.org>.
Github user britter commented on the pull request:
https://github.com/apache/commons-lang/pull/78#issuecomment-98808768
@CodingFabian @salyh agreed. Very good work! Let's resolve the last two comments from Fabian and then we can integrate it.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by coveralls <gi...@git.apache.org>.
Github user coveralls commented on the pull request:
https://github.com/apache/commons-lang/pull/78#issuecomment-98551006
[](https://coveralls.io/builds/2477412)
Coverage increased (+0.04%) to 93.35% when pulling **6f653f6bb82757963a87d725808c080109e7e3e8 on salyh:threadutilsmaster** into **8e7ea70a33ecb8db5207849ea6371b4a5a8ffd26 on apache:master**.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by coveralls <gi...@git.apache.org>.
Github user coveralls commented on the pull request:
https://github.com/apache/commons-lang/pull/78#issuecomment-98604244
[](https://coveralls.io/builds/2478890)
Coverage increased (+0.04%) to 93.35% when pulling **1974c83561d09cf73a575f992fbd8416e3354d20 on salyh:threadutilsmaster** into **8e7ea70a33ecb8db5207849ea6371b4a5a8ffd26 on apache:master**.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by peteryhwong <gi...@git.apache.org>.
Github user peteryhwong commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29562446
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,459 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, new NamePredicate(threadName)));
--- End diff --
The NamePredicate object is the same in every iteration. Hence only one instance is required outside of the for loop.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by salyh <gi...@git.apache.org>.
Github user salyh commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29612060
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ NamePredicate threadNamePredicate = null;
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, threadNamePredicate==null?(threadNamePredicate=new NamePredicate(threadName)):threadNamePredicate));
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Return active thread groups with the specified group name.
+ *
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
+ return findThreadGroups(new NamePredicate(threadGroupName));
+ }
+
+ /**
+ * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
+ *
+ * @return 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
+ *
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> getAllThreadGroups() {
+ return findThreadGroups(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return the system thread group (sometimes also referred as "root thread group").
+ *
+ * @return 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
+ */
+ public static ThreadGroup getSystemThreadGroup() {
+ ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+ while(threadGroup.getParent() != null) {
+ threadGroup = threadGroup.getParent();
+ }
+ return threadGroup;
+ }
+
+ /**
+ * Return all active threads (A thread is active if it has been started and has not yet died).
+ *
+ * @return all active threads. The collection returned is always unmodifiable.
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> getAllThreads() {
+ return findThreads(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return active threads with the specified name.
+ *
+ * @param threadName The thread name
+ * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
+ * @throws IllegalArgumentException if the specified name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName) {
+ return findThreads(new NamePredicate(threadName));
+ }
+
+ /**
+ * Return the active thread with the specified id.
+ *
+ * @param threadId The thread id
+ * @return The thread with the specified id or {@code null} if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId) {
+ final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
+
+ if(result.isEmpty()) {
+ return null;
+ } else {
+ return result.iterator().next();
+ }
+
+ }
+
+ /**
+ * <p>
+ * ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
+ * {@code ThreadUtils.getAllThreads()}
+ * </p>
+ * <p>
+ * This constructor is public to permit tools that require a JavaBean instance to operate.
+ * </p>
+ */
+ public ThreadUtils() {
+ super();
+ }
+
+ /**
+ * A predicate for selecting threads.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{
+
+ /**
+ * Evaluates this predicate on the given thread.
+ * @param thread the thread
+ * @return {@code true} if the thread matches the predicate, otherwise {@code false}
+ */
+ boolean test(Thread thread);
+ }
+
+ /**
+ * A predicate for selecting threadgroups.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{
+
+ /**
+ * Evaluates this predicate on the given threadgroup.
+ * @param threadGroup the threadgroup
+ * @return {@code true} if the threadGroup matches the predicate, otherwise {@code false}
+ */
+ boolean test(ThreadGroup threadGroup);
+ }
+
+ /**
+ * Predicate which always returns true.
+ */
+ public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
+
+ /**
+ * A predicate implementation which always returns true.
+ */
+ private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{
+
+ private AlwaysTruePredicate() {
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final ThreadGroup threadGroup) {
+ return true;
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final Thread thread) {
+ return true;
+ }
+
+ }
+
+ /**
+ * A predicate implementation which matches a thread or threadgroup name.
+ */
+ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate {
+
+ private final String name;
+
+ /**
+ * Predicate constructor
+ *
+ * @param name thread or threadgroup name
+ * @throws IllegalArgumentException if the name is {@code null}
+ */
+ public NamePredicate(final String name) {
+ super();
+ if (name == null) {
+ throw new IllegalArgumentException("The name must not be null");
+ }
+ this.name = name;
+ }
+
+ @Override
+ public boolean test(final ThreadGroup threadGroup) {
+ return threadGroup != null && threadGroup.getName().equals(name);
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getName().equals(name);
+ }
+ }
+
+ /**
+ * A predicate implementation which matches a thread id.
+ */
+ public static class ThreadIdPredicate implements ThreadPredicate {
+
+ private final long threadId;
+
+ /**
+ * Predicate constructor
+ *
+ * @param threadId the threadId to match
+ * @throws IllegalArgumentException if the threadId is zero or negative
+ */
+ public ThreadIdPredicate(final long threadId) {
+ super();
+ if (threadId <= 0) {
+ throw new IllegalArgumentException("The thread id must be greater than zero");
+ }
+ this.threadId = threadId;
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getId() == threadId;
+ }
+ }
+
+ /**
+ * Select all active threads which match the given predicate.
+ *
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads matching the given predicate
+ *
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreads(final ThreadPredicate predicate){
+ return findThreads(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate.
+ *
+ * @param predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){
+ return findThreadGroups(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) {
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeCount();
+ Thread[] threads;
+ do {
+ threads = new Thread[count + (count >> 1) + 1];
+ count = group.enumerate(threads, recurse);
+ } while (count >= threads.length);
+
+ final List<Thread> result = new ArrayList<Thread>(count);
+ for (int i = 0; i < count; ++i) {
+ if (predicate.test(threads[i])) {
+ result.add(threads[i]);
+ }
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threadgroups in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups which match the given predicate and which is a subgroup of the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeGroupCount();
+ ThreadGroup[] threadGroups;
+ do {
+ threadGroups = new ThreadGroup[count + (count>>1) + 1];
--- End diff --
this was a suggestion from @chonton. IMHO its better (than using *2) because array size does not grow so fast
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by CodingFabian <gi...@git.apache.org>.
Github user CodingFabian commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29569670
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ NamePredicate threadNamePredicate = null;
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, threadNamePredicate==null?(threadNamePredicate=new NamePredicate(threadName)):threadNamePredicate));
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Return active thread groups with the specified group name.
+ *
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
+ return findThreadGroups(new NamePredicate(threadGroupName));
+ }
+
+ /**
+ * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
+ *
+ * @return 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
+ *
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> getAllThreadGroups() {
+ return findThreadGroups(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return the system thread group (sometimes also referred as "root thread group").
+ *
+ * @return 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
+ */
+ public static ThreadGroup getSystemThreadGroup() {
+ ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+ while(threadGroup.getParent() != null) {
+ threadGroup = threadGroup.getParent();
+ }
+ return threadGroup;
+ }
+
+ /**
+ * Return all active threads (A thread is active if it has been started and has not yet died).
+ *
+ * @return all active threads. The collection returned is always unmodifiable.
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> getAllThreads() {
+ return findThreads(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return active threads with the specified name.
+ *
+ * @param threadName The thread name
+ * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
+ * @throws IllegalArgumentException if the specified name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName) {
+ return findThreads(new NamePredicate(threadName));
+ }
+
+ /**
+ * Return the active thread with the specified id.
+ *
+ * @param threadId The thread id
+ * @return The thread with the specified id or {@code null} if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId) {
+ final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
+
+ if(result.isEmpty()) {
+ return null;
+ } else {
+ return result.iterator().next();
+ }
+
+ }
+
+ /**
+ * <p>
+ * ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
+ * {@code ThreadUtils.getAllThreads()}
+ * </p>
+ * <p>
+ * This constructor is public to permit tools that require a JavaBean instance to operate.
+ * </p>
+ */
+ public ThreadUtils() {
+ super();
+ }
+
+ /**
+ * A predicate for selecting threads.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{
+
+ /**
+ * Evaluates this predicate on the given thread.
+ * @param thread the thread
+ * @return {@code true} if the thread matches the predicate, otherwise {@code false}
+ */
+ boolean test(Thread thread);
+ }
+
+ /**
+ * A predicate for selecting threadgroups.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{
+
+ /**
+ * Evaluates this predicate on the given threadgroup.
+ * @param threadGroup the threadgroup
+ * @return {@code true} if the threadGroup matches the predicate, otherwise {@code false}
+ */
+ boolean test(ThreadGroup threadGroup);
+ }
+
+ /**
+ * Predicate which always returns true.
+ */
+ public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
+
+ /**
+ * A predicate implementation which always returns true.
+ */
+ private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{
+
+ private AlwaysTruePredicate() {
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final ThreadGroup threadGroup) {
+ return true;
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final Thread thread) {
+ return true;
+ }
+
+ }
+
+ /**
+ * A predicate implementation which matches a thread or threadgroup name.
+ */
+ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate {
+
+ private final String name;
+
+ /**
+ * Predicate constructor
+ *
+ * @param name thread or threadgroup name
+ * @throws IllegalArgumentException if the name is {@code null}
+ */
+ public NamePredicate(final String name) {
+ super();
+ if (name == null) {
+ throw new IllegalArgumentException("The name must not be null");
+ }
+ this.name = name;
+ }
+
+ @Override
+ public boolean test(final ThreadGroup threadGroup) {
+ return threadGroup != null && threadGroup.getName().equals(name);
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getName().equals(name);
+ }
+ }
+
+ /**
+ * A predicate implementation which matches a thread id.
+ */
+ public static class ThreadIdPredicate implements ThreadPredicate {
+
+ private final long threadId;
+
+ /**
+ * Predicate constructor
+ *
+ * @param threadId the threadId to match
+ * @throws IllegalArgumentException if the threadId is zero or negative
+ */
+ public ThreadIdPredicate(final long threadId) {
+ super();
+ if (threadId <= 0) {
+ throw new IllegalArgumentException("The thread id must be greater than zero");
+ }
+ this.threadId = threadId;
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getId() == threadId;
+ }
+ }
+
+ /**
+ * Select all active threads which match the given predicate.
+ *
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads matching the given predicate
+ *
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreads(final ThreadPredicate predicate){
+ return findThreads(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate.
+ *
+ * @param predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){
+ return findThreadGroups(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) {
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeCount();
+ Thread[] threads;
+ do {
+ threads = new Thread[count + (count >> 1) + 1];
+ count = group.enumerate(threads, recurse);
+ } while (count >= threads.length);
+
+ final List<Thread> result = new ArrayList<Thread>(count);
+ for (int i = 0; i < count; ++i) {
+ if (predicate.test(threads[i])) {
+ result.add(threads[i]);
+ }
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threadgroups in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups which match the given predicate and which is a subgroup of the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeGroupCount();
+ ThreadGroup[] threadGroups;
+ do {
+ threadGroups = new ThreadGroup[count + (count>>1) + 1];
--- End diff --
why the magic right shift? is this based on experience?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by CodingFabian <gi...@git.apache.org>.
Github user CodingFabian commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29613233
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ NamePredicate threadNamePredicate = null;
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, threadNamePredicate==null?(threadNamePredicate=new NamePredicate(threadName)):threadNamePredicate));
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Return active thread groups with the specified group name.
+ *
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
+ return findThreadGroups(new NamePredicate(threadGroupName));
+ }
+
+ /**
+ * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
+ *
+ * @return 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
+ *
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> getAllThreadGroups() {
+ return findThreadGroups(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return the system thread group (sometimes also referred as "root thread group").
+ *
+ * @return 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
+ */
+ public static ThreadGroup getSystemThreadGroup() {
+ ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+ while(threadGroup.getParent() != null) {
+ threadGroup = threadGroup.getParent();
+ }
+ return threadGroup;
+ }
+
+ /**
+ * Return all active threads (A thread is active if it has been started and has not yet died).
+ *
+ * @return all active threads. The collection returned is always unmodifiable.
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> getAllThreads() {
+ return findThreads(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return active threads with the specified name.
+ *
+ * @param threadName The thread name
+ * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
+ * @throws IllegalArgumentException if the specified name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName) {
+ return findThreads(new NamePredicate(threadName));
+ }
+
+ /**
+ * Return the active thread with the specified id.
+ *
+ * @param threadId The thread id
+ * @return The thread with the specified id or {@code null} if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId) {
+ final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
+
+ if(result.isEmpty()) {
+ return null;
+ } else {
+ return result.iterator().next();
+ }
+
+ }
+
+ /**
+ * <p>
+ * ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
+ * {@code ThreadUtils.getAllThreads()}
+ * </p>
+ * <p>
+ * This constructor is public to permit tools that require a JavaBean instance to operate.
+ * </p>
+ */
+ public ThreadUtils() {
+ super();
+ }
+
+ /**
+ * A predicate for selecting threads.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{
+
+ /**
+ * Evaluates this predicate on the given thread.
+ * @param thread the thread
+ * @return {@code true} if the thread matches the predicate, otherwise {@code false}
+ */
+ boolean test(Thread thread);
+ }
+
+ /**
+ * A predicate for selecting threadgroups.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{
+
+ /**
+ * Evaluates this predicate on the given threadgroup.
+ * @param threadGroup the threadgroup
+ * @return {@code true} if the threadGroup matches the predicate, otherwise {@code false}
+ */
+ boolean test(ThreadGroup threadGroup);
+ }
+
+ /**
+ * Predicate which always returns true.
+ */
+ public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
+
+ /**
+ * A predicate implementation which always returns true.
+ */
+ private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{
+
+ private AlwaysTruePredicate() {
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final ThreadGroup threadGroup) {
+ return true;
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final Thread thread) {
+ return true;
+ }
+
+ }
+
+ /**
+ * A predicate implementation which matches a thread or threadgroup name.
+ */
+ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate {
+
+ private final String name;
+
+ /**
+ * Predicate constructor
+ *
+ * @param name thread or threadgroup name
+ * @throws IllegalArgumentException if the name is {@code null}
+ */
+ public NamePredicate(final String name) {
+ super();
+ if (name == null) {
+ throw new IllegalArgumentException("The name must not be null");
+ }
+ this.name = name;
+ }
+
+ @Override
+ public boolean test(final ThreadGroup threadGroup) {
+ return threadGroup != null && threadGroup.getName().equals(name);
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getName().equals(name);
+ }
+ }
+
+ /**
+ * A predicate implementation which matches a thread id.
+ */
+ public static class ThreadIdPredicate implements ThreadPredicate {
+
+ private final long threadId;
+
+ /**
+ * Predicate constructor
+ *
+ * @param threadId the threadId to match
+ * @throws IllegalArgumentException if the threadId is zero or negative
+ */
+ public ThreadIdPredicate(final long threadId) {
+ super();
+ if (threadId <= 0) {
+ throw new IllegalArgumentException("The thread id must be greater than zero");
+ }
+ this.threadId = threadId;
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getId() == threadId;
+ }
+ }
+
+ /**
+ * Select all active threads which match the given predicate.
+ *
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads matching the given predicate
+ *
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreads(final ThreadPredicate predicate){
+ return findThreads(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate.
+ *
+ * @param predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){
+ return findThreadGroups(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) {
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeCount();
+ Thread[] threads;
+ do {
+ threads = new Thread[count + (count >> 1) + 1];
+ count = group.enumerate(threads, recurse);
+ } while (count >= threads.length);
+
+ final List<Thread> result = new ArrayList<Thread>(count);
+ for (int i = 0; i < count; ++i) {
+ if (predicate.test(threads[i])) {
+ result.add(threads[i]);
+ }
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threadgroups in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups which match the given predicate and which is a subgroup of the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeGroupCount();
+ ThreadGroup[] threadGroups;
+ do {
+ threadGroups = new ThreadGroup[count + (count>>1) + 1];
--- End diff --
ok, maybe add a comment, so that a maintainer in 3 years will not say WTF? :)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by coveralls <gi...@git.apache.org>.
Github user coveralls commented on the pull request:
https://github.com/apache/commons-lang/pull/78#issuecomment-98827173
[](https://coveralls.io/builds/2483762)
Coverage increased (+0.03%) to 93.34% when pulling **061450a927ba1cc65f7b984f4ff41866ec2eceac on salyh:threadutilsmaster** into **8e7ea70a33ecb8db5207849ea6371b4a5a8ffd26 on apache:master**.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by salyh <gi...@git.apache.org>.
Github user salyh commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/78#discussion_r29614321
--- Diff: src/main/java/org/apache/commons/lang3/ThreadUtils.java ---
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
+ * </p>
+ * <p>
+ * #ThreadSafe#
+ * </p>
+ *
+ * @see java.lang.Thread
+ * @see java.lang.ThreadGroup
+ * @since 3.5
+ * @version $Id$
+ */
+public class ThreadUtils {
+
+ /**
+ * Return the active thread with the specified id if it belong's to the specified thread group.
+ *
+ * @param threadId The thread id
+ * @param threadGroup The thread group
+ * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
+ if (threadGroup == null) {
+ throw new IllegalArgumentException("The thread group must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
+ *
+ * @param threadId The thread id
+ * @param threadGroupName The thread group name
+ * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
+ * {@code null} is returned if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId, final String threadGroupName) {
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+ final Thread thread = findThreadById(threadId);
+ if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
+ return thread;
+ }
+ return null;
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a specified thread group.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group
+ * @return 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 IllegalArgumentException if the specified thread name or group is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
+ return findThreads(threadGroup, false, new NamePredicate(threadName));
+ }
+
+ /**
+ * Return active threads with the specified name if they belong to a thread group with the specified group name.
+ *
+ * @param threadName The thread name
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if the specified thread name or group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
+ if (threadName == null) {
+ throw new IllegalArgumentException("The thread name must not be null");
+ }
+ if (threadGroupName == null) {
+ throw new IllegalArgumentException("The thread group name must not be null");
+ }
+
+ final Collection<Thread> result = new ArrayList<Thread>();
+ NamePredicate threadNamePredicate = null;
+ for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) {
+ result.addAll(findThreads(group, false, threadNamePredicate==null?(threadNamePredicate=new NamePredicate(threadName)):threadNamePredicate));
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Return active thread groups with the specified group name.
+ *
+ * @param threadGroupName The thread group name
+ * @return 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 IllegalArgumentException if group name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
+ return findThreadGroups(new NamePredicate(threadGroupName));
+ }
+
+ /**
+ * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
+ *
+ * @return 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
+ *
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> getAllThreadGroups() {
+ return findThreadGroups(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return the system thread group (sometimes also referred as "root thread group").
+ *
+ * @return 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
+ */
+ public static ThreadGroup getSystemThreadGroup() {
+ ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
+ while(threadGroup.getParent() != null) {
+ threadGroup = threadGroup.getParent();
+ }
+ return threadGroup;
+ }
+
+ /**
+ * Return all active threads (A thread is active if it has been started and has not yet died).
+ *
+ * @return all active threads. The collection returned is always unmodifiable.
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> getAllThreads() {
+ return findThreads(ALWAYS_TRUE_PREDICATE);
+ }
+
+ /**
+ * Return active threads with the specified name.
+ *
+ * @param threadName The thread name
+ * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
+ * @throws IllegalArgumentException if the specified name is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreadsByName(final String threadName) {
+ return findThreads(new NamePredicate(threadName));
+ }
+
+ /**
+ * Return the active thread with the specified id.
+ *
+ * @param threadId The thread id
+ * @return The thread with the specified id or {@code null} if no such thread exists
+ * @throws IllegalArgumentException if the specified id is zero or negative
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Thread findThreadById(final long threadId) {
+ final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
+
+ if(result.isEmpty()) {
+ return null;
+ } else {
+ return result.iterator().next();
+ }
+
+ }
+
+ /**
+ * <p>
+ * ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
+ * {@code ThreadUtils.getAllThreads()}
+ * </p>
+ * <p>
+ * This constructor is public to permit tools that require a JavaBean instance to operate.
+ * </p>
+ */
+ public ThreadUtils() {
+ super();
+ }
+
+ /**
+ * A predicate for selecting threads.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{
+
+ /**
+ * Evaluates this predicate on the given thread.
+ * @param thread the thread
+ * @return {@code true} if the thread matches the predicate, otherwise {@code false}
+ */
+ boolean test(Thread thread);
+ }
+
+ /**
+ * A predicate for selecting threadgroups.
+ */
+ //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
+ public static interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{
+
+ /**
+ * Evaluates this predicate on the given threadgroup.
+ * @param threadGroup the threadgroup
+ * @return {@code true} if the threadGroup matches the predicate, otherwise {@code false}
+ */
+ boolean test(ThreadGroup threadGroup);
+ }
+
+ /**
+ * Predicate which always returns true.
+ */
+ public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
+
+ /**
+ * A predicate implementation which always returns true.
+ */
+ private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{
+
+ private AlwaysTruePredicate() {
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final ThreadGroup threadGroup) {
+ return true;
+ }
+
+ @Override
+ public boolean test(@SuppressWarnings("unused") final Thread thread) {
+ return true;
+ }
+
+ }
+
+ /**
+ * A predicate implementation which matches a thread or threadgroup name.
+ */
+ public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate {
+
+ private final String name;
+
+ /**
+ * Predicate constructor
+ *
+ * @param name thread or threadgroup name
+ * @throws IllegalArgumentException if the name is {@code null}
+ */
+ public NamePredicate(final String name) {
+ super();
+ if (name == null) {
+ throw new IllegalArgumentException("The name must not be null");
+ }
+ this.name = name;
+ }
+
+ @Override
+ public boolean test(final ThreadGroup threadGroup) {
+ return threadGroup != null && threadGroup.getName().equals(name);
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getName().equals(name);
+ }
+ }
+
+ /**
+ * A predicate implementation which matches a thread id.
+ */
+ public static class ThreadIdPredicate implements ThreadPredicate {
+
+ private final long threadId;
+
+ /**
+ * Predicate constructor
+ *
+ * @param threadId the threadId to match
+ * @throws IllegalArgumentException if the threadId is zero or negative
+ */
+ public ThreadIdPredicate(final long threadId) {
+ super();
+ if (threadId <= 0) {
+ throw new IllegalArgumentException("The thread id must be greater than zero");
+ }
+ this.threadId = threadId;
+ }
+
+ @Override
+ public boolean test(final Thread thread) {
+ return thread != null && thread.getId() == threadId;
+ }
+ }
+
+ /**
+ * Select all active threads which match the given predicate.
+ *
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads matching the given predicate
+ *
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<Thread> findThreads(final ThreadPredicate predicate){
+ return findThreads(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate.
+ *
+ * @param predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate
+ * @throws IllegalArgumentException if the predicate is null
+ * @throws SecurityException
+ * if the current thread cannot access 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
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){
+ return findThreadGroups(getSystemThreadGroup(), true, predicate);
+ }
+
+ /**
+ * Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) {
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeCount();
+ Thread[] threads;
+ do {
+ threads = new Thread[count + (count >> 1) + 1];
+ count = group.enumerate(threads, recurse);
+ } while (count >= threads.length);
+
+ final List<Thread> result = new ArrayList<Thread>(count);
+ for (int i = 0; i < count; ++i) {
+ if (predicate.test(threads[i])) {
+ result.add(threads[i]);
+ }
+ }
+ return Collections.unmodifiableCollection(result);
+ }
+
+ /**
+ * Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
+ *
+ * @param group the thread group
+ * @param recurse if {@code true} then evaluate the predicate recursively on all threadgroups in all subgroups of the given group
+ * @param predicate the predicate
+ * @return An unmodifiable {@code Collection} of active threadgroups which match the given predicate and which is a subgroup of the given thread group
+ * @throws IllegalArgumentException if the given group or predicate is null
+ * @throws SecurityException if the current thread cannot modify
+ * thread groups from this thread's thread group up to the system thread group
+ */
+ public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){
+ if (group == null) {
+ throw new IllegalArgumentException("The group must not be null");
+ }
+ if (predicate == null) {
+ throw new IllegalArgumentException("The predicate must not be null");
+ }
+
+ int count = group.activeGroupCount();
+ ThreadGroup[] threadGroups;
+ do {
+ threadGroups = new ThreadGroup[count + (count>>1) + 1];
--- End diff --
replaced the magic with something more readable (count/2), should to the same
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Posted by CodingFabian <gi...@git.apache.org>.
Github user CodingFabian commented on the pull request:
https://github.com/apache/commons-lang/pull/78#issuecomment-98610476
Thank you @salyh for your work on this. Overall I like the API and the implementation. I added two comments.
Also it looks like your formatter settings are not used. some spaces are missing (like i=0 instead of i = 0)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---