1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.distribution;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /***
23 * A utility class for distributed tests
24 *
25 * @author Greg Luck
26 * @version $Id: JVMUtil.html 13146 2011-08-01 17:12:39Z oletizi $
27 */
28 public final class JVMUtil {
29 private static final float JDK_1_5 = 1.5f;
30 private static final int FIRST_THREE_CHARS = 3;
31
32 /***
33 * Utility class. No constructor
34 */
35 private JVMUtil() {
36
37 }
38
39
40 /***
41 * Lists all the threads in the VM
42 *
43 * @return a List of type Thread
44 */
45 public static List<Thread> enumerateThreads() {
46
47 /***
48 * A class for visiting threads
49 */
50 class ThreadVisitor {
51
52 private final List<Thread> threadList = new ArrayList<Thread>();
53
54
55 private void visit(ThreadGroup group, int level) {
56
57 int numThreads = group.activeCount();
58 Thread[] threads = new Thread[numThreads * 2];
59 numThreads = group.enumerate(threads, false);
60
61
62 for (int i = 0; i < numThreads; i++) {
63
64 Thread thread = threads[i];
65 threadList.add(thread);
66 }
67
68
69 int numGroups = group.activeGroupCount();
70 ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
71 numGroups = group.enumerate(groups, false);
72
73
74 for (int i = 0; i < numGroups; i++) {
75 visit(groups[i], level + 1);
76 }
77 }
78 }
79
80
81 ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
82 while (root.getParent() != null) {
83 root = root.getParent();
84 }
85
86
87 ThreadVisitor visitor = new ThreadVisitor();
88 visitor.visit(root, 0);
89 return visitor.threadList;
90 }
91
92 }