From 1974c83561d09cf73a575f992fbd8416e3354d20 Mon Sep 17 00:00:00 2001 From: Hendrik Saly Date: Mon, 4 May 2015 09:03:21 +0200 Subject: [PATCH] incorporated feedback from Peter --- .../java/org/apache/commons/lang3/ThreadUtils.java | 10 ++++++---- .../java/org/apache/commons/lang3/ThreadUtilsTest.java | 10 +++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java index 602facee9..758baf944 100644 --- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java +++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java @@ -127,8 +127,9 @@ public class ThreadUtils { } final Collection result = new ArrayList(); + NamePredicate threadNamePredicate = null; for(final ThreadGroup group : findThreadGroups(new NamePredicate(threadGroupName))) { - result.addAll(findThreads(group, false, new NamePredicate(threadName))); + result.addAll(findThreads(group, false, threadNamePredicate==null?(threadNamePredicate=new NamePredicate(threadName)):threadNamePredicate)); } return Collections.unmodifiableCollection(result); } @@ -223,7 +224,7 @@ public class ThreadUtils { public static Thread findThreadById(final long threadId) { final Collection result = findThreads(new ThreadIdPredicate(threadId)); - if(!result.iterator().hasNext()) { + if(result.isEmpty()) { return null; } else { return result.iterator().next(); @@ -406,7 +407,6 @@ public class ThreadUtils { throw new IllegalArgumentException("The predicate must not be null"); } - final List result = new ArrayList(); int count = group.activeCount(); Thread[] threads; do { @@ -414,6 +414,7 @@ public class ThreadUtils { count = group.enumerate(threads, recurse); } while (count >= threads.length); + final List result = new ArrayList(count); for (int i = 0; i < count; ++i) { if (predicate.test(threads[i])) { result.add(threads[i]); @@ -440,7 +441,7 @@ public class ThreadUtils { if (predicate == null) { throw new IllegalArgumentException("The predicate must not be null"); } - final List result = new ArrayList(); + int count = group.activeGroupCount(); ThreadGroup[] threadGroups; do { @@ -449,6 +450,7 @@ public class ThreadUtils { } while(count >= threadGroups.length); + final List result = new ArrayList(count); for(int i = 0; i threadGroups = Arrays.asList(threadGroup1,threadGroup2,threadGroup3,threadGroup4,threadGroup5,threadGroup6); + final ThreadGroup threadGroup7 = new ThreadGroup(threadGroup4, "thread_group_7__"); + final ThreadGroup threadGroup7Doubled = new ThreadGroup(threadGroup4, "thread_group_7__"); + final List threadGroups = Arrays.asList(threadGroup1,threadGroup2,threadGroup3,threadGroup4,threadGroup5,threadGroup6, threadGroup7, threadGroup7Doubled); final Thread t1 = new TestThread("thread1_X__"); final Thread t2 = new TestThread(threadGroup1, "thread2_X__"); @@ -308,7 +310,9 @@ public class ThreadUtilsTest { final Thread t8 = new TestThread(threadGroup4, "thread8_X__"); final Thread t9 = new TestThread(threadGroup6, "thread9_X__"); final Thread t10 = new TestThread(threadGroup3, "thread10_X__"); - final List threads = Arrays.asList(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10); + final Thread t11 = new TestThread(threadGroup7, "thread11_X__"); + final Thread t11Doubled = new TestThread(threadGroup7Doubled, "thread11_X__"); + final List threads = Arrays.asList(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10, t11, t11Doubled); try { for (final Iterator iterator = threads.iterator(); iterator.hasNext();) { @@ -320,7 +324,7 @@ public class ThreadUtilsTest { assertTrue(ThreadUtils.findThreads(ThreadUtils.ALWAYS_TRUE_PREDICATE).size() >= 11); assertEquals(1, ThreadUtils.findThreadsByName(t4.getName(), threadGroup3.getName()).size()); assertEquals(0, ThreadUtils.findThreadsByName(t4.getName(), threadGroup2.getName()).size()); - + assertEquals(2, ThreadUtils.findThreadsByName(t11.getName(), threadGroup7.getName()).size()); }finally { for (final Iterator iterator = threads.iterator(); iterator.hasNext();) { final Thread thread = (Thread) iterator.next();