commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From CodingFabian <...@git.apache.org>
Subject [GitHub] commons-lang pull request: Add ThreadUtils (2nd PR)
Date Mon, 04 May 2015 07:32:02 GMT
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.
---

Mime
View raw message