incorporated feedback from Peter

This commit is contained in:
Hendrik Saly 2015-05-04 09:03:21 +02:00
parent 6f653f6bb8
commit 1974c83561
2 changed files with 13 additions and 7 deletions

View File

@ -127,8 +127,9 @@ public class ThreadUtils {
}
final Collection<Thread> result = new ArrayList<Thread>();
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<Thread> 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<Thread> result = new ArrayList<Thread>();
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<Thread> result = new ArrayList<Thread>(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<ThreadGroup> result = new ArrayList<ThreadGroup>();
int count = group.activeGroupCount();
ThreadGroup[] threadGroups;
do {
@ -449,6 +450,7 @@ public class ThreadUtils {
}
while(count >= threadGroups.length);
final List<ThreadGroup> result = new ArrayList<ThreadGroup>(count);
for(int i = 0; i<count; ++i) {
if(predicate.test(threadGroups[i])) {
result.add(threadGroups[i]);

View File

@ -296,7 +296,9 @@ public class ThreadUtilsTest {
final ThreadGroup threadGroup4 = new ThreadGroup(threadGroup2, "thread_group_4__");
final ThreadGroup threadGroup5 = new ThreadGroup(threadGroup1, "thread_group_5__");
final ThreadGroup threadGroup6 = new ThreadGroup(threadGroup4, "thread_group_6__");
final List<ThreadGroup> 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<ThreadGroup> 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<Thread> 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<Thread> 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();