Created
August 6, 2012 18:20
-
-
Save arunreddy/3277312 to your computer and use it in GitHub Desktop.
Threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String getThreadStatus() | |
{ | |
StringBuffer buf = new StringBuffer(); | |
Thread[] daemonThreads = getAllDaemonThreads(); | |
for (Thread thread : daemonThreads) { | |
if (thread.getName().contains("Solrj")) { | |
buf.append(thread.getName() + "--" + thread + "\n"); | |
} | |
} | |
return buf.toString(); | |
} | |
ThreadGroup rootThreadGroup = null; | |
ThreadGroup getRootThreadGroup() | |
{ | |
if (rootThreadGroup != null) | |
return rootThreadGroup; | |
ThreadGroup tg = Thread.currentThread().getThreadGroup(); | |
ThreadGroup ptg; | |
while ((ptg = tg.getParent()) != null) | |
tg = ptg; | |
return tg; | |
} | |
Thread[] getAllThreads() | |
{ | |
final ThreadGroup root = getRootThreadGroup(); | |
final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); | |
int nAlloc = thbean.getThreadCount(); | |
int n = 0; | |
Thread[] threads; | |
do { | |
nAlloc *= 2; | |
threads = new Thread[nAlloc]; | |
n = root.enumerate(threads, true); | |
} while (n == nAlloc); | |
return java.util.Arrays.copyOf(threads, n); | |
} | |
Thread[] getAllDaemonThreads() | |
{ | |
final Thread[] allThreads = getAllThreads(); | |
final Thread[] daemons = new Thread[allThreads.length]; | |
int nDaemon = 0; | |
for (Thread thread : allThreads) | |
if (thread.isDaemon()) | |
daemons[nDaemon++] = thread; | |
return java.util.Arrays.copyOf(daemons, nDaemon); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment