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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to