Article updated on

Java JSP Examples to Display Thread Status.

Sometimes it's useful to know that is some threads status. I normally use JConsole to monitor but if you can't use this technology and you need to know what is going on with the threads an easy trick is to upload a JSP to the server. Here are some good examples for that.

Example 1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Set" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
int i = 0;
for(Thread threadaux :threadArray){
    i++;
    out.print(i+" "+threadaux.getName()+"<br/>");
}
%>
</body>

 

Example 2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintStream,
                 java.lang.management.ManagementFactory,
                 java.lang.management.ThreadInfo,
                 java.lang.management.ThreadMXBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quick and Dirty JSP Thread Report</title>
</head>
<body>
<%!
private static ThreadMXBean thMxBean =
ManagementFactory.getThreadMXBean();
private static String getTaskName(long id, String name) {
    if (name == null) {
        return Long.toString(id);
    }
    return id + " (" + name + ")<br>";
}
%><%
final int STACK_DEPTH = 20;
boolean contention = thMxBean.isThreadContentionMonitoringEnabled();
long[] threadIds = thMxBean.getAllThreadIds();
out.println("Process Thread Dump: <br/>");
out.println(threadIds.length + " active threads<br/>");
for (long tid : threadIds) {
    ThreadInfo info = thMxBean.getThreadInfo(tid, STACK_DEPTH);
    if (info == null) {
        out.println("  Inactive");
        continue;
    }
    out.println(" <br/><br/>Thread "
            + getTaskName(info.getThreadId(), info.getThreadName()));
    Thread.State state = info.getThreadState();
    out.println("  State: " + state);
    out.println("  <br/>Blocked count: " + info.getBlockedCount());
    out.println("  <br/>Waited count: " + info.getWaitedCount());
    if (contention) {
        out.println("  <br/>Blocked time: " + info.getBlockedTime());
        out.println(" <br/> Waited time: " + info.getWaitedTime());
    }
    if (state == Thread.State.WAITING) {
        out.println("  Waiting on " + info.getLockName());
    } else if (state == Thread.State.BLOCKED) {
        out.println("  Blocked on " + info.getLockName());
        out.println("  Blocked by <br/>"
                + getTaskName(info.getLockOwnerId(),
                        info.getLockOwnerName()));
    }
    out.println("  <br/>Stack: ");
    for (StackTraceElement frame : info.getStackTrace()) {
        out.println("    " + frame.toString());
    }
}%>
</body>
</html>

 

Example 3

<%@ page language="java" import="java.util.*,java.lang.Thread.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%--
Author:- Ramakanta(rks2286(at)gmail(dot)com)
Date Created: Mar 14 2010
PurPose: Online Java Thread Dump
Minimum Java version Required: 1.5.x
--%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<title>Thread Dump</title>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolor="#000000">
<tr>
<td bgcolor="#E7E7EF" bordercolor="#000000" align="center" nowrap>
<font face="Verdana" size="+1">Thread Dumps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>
</td>
</tr>
<tr>
<td bgcolor="#E7E7EF" bordercolor="#000000">
<%
out.print("---------------------------START-----------------------------------------<br>");
out.print("Generating Thread-Dump At:" + (new java.util.Date()).toString() + "<BR>");
out.println("---------------------------------------------------------------------<br>");
Map map = Thread.getAllStackTraces();
Iterator itr = map.keySet().iterator();
while (itr.hasNext()) {
Thread t = (Thread)itr.next();
StackTraceElement[] elem = (StackTraceElement[])map.get(t);
out.print("\"" + t.getName() + "\"");
out.print(" Priority=" + t.getPriority());
out.print(" Thread Id=" + t.getId());
State s = t.getState();
String state = null;
String color = "000000";
String GREEN = "00FF00";
String RED = "FF0000";
String ORANGE = "FCA742";
switch(s) {
case NEW: state ="NEW"; color = GREEN; break;
case BLOCKED: state = "BLOCKED"; color = RED; break;
case RUNNABLE: state = "RUNNABLE"; color = GREEN; break;
case TERMINATED: state = "TERMINATED"; break;
case TIMED_WAITING: state = "TIME WAITING"; color = ORANGE; break;
case WAITING: state = "WAITING"; color = RED; break;
}
out.print("<font color=\"" + color + "\"> In State :</font>");
out.println(" " + state + "<BR>");
for (int i=0; i < elem.length; i++) {
out.println("  at ");
out.print(elem[i].toString());
out.println("<BR>");
}
out.println("--------------------------------------------------------------------------<br>");
}
out.print("----------------------------FINISH--------------------------------------<br>");
out.print("Generated Thread-Dump At:" + (new java.util.Date()).toString() + "<BR>");
out.println("---------------------------------------------------------------------<br>");
%>
</td>
</tr>
</table>
</body>
</html>