Skip to content

Instantly share code, notes, and snippets.

@arunreddy
Created August 6, 2012 18:20
Show Gist options
  • Save arunreddy/3277312 to your computer and use it in GitHub Desktop.
Save arunreddy/3277312 to your computer and use it in GitHub Desktop.
Threads
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