%@ page import="java.lang.management.*,java.lang.Thread.State,java.util.Map,java.util.HashMap,java.util.HashSet,java.util.Date"%>
<%@ page import="java.text.DecimalFormat,java.text.SimpleDateFormat"%>
<%!
/************** page variable and method define start ************************/
final String versionAndTitle="Thread Checker v.2008.12.16.";
final int[] optionSeconds={1,3,5,10,20,30,60,600};
final String numberPattern="###,###,###,###.###";
final DecimalFormat format=new DecimalFormat(numberPattern);
public String getTimeString(long nanoSecond) {
long tempMilli=nanoSecond/1000000;
if(tempMilli<1000) {
return format.format(tempMilli/1000.0)+" ms";
} else if(tempMilli<60000) {
return format.format(tempMilli/1000.0) +" sec";
} else {
return format.format(tempMilli/60000.0) +" min";
}
}
final String pageWidth="1024";//"100%";
final String pageWidth2="1000";//"95%";
final SimpleDateFormat saveFileFormat =new SimpleDateFormat("yyyy-MM-dd_HH mm ss");
/************** page variable and method define end ************************/
%>
<%=versionAndTitle %> by Sangmin, Lee
<%
String saveFileName=saveFileFormat.format(new Date());
/************** request value check start ************************/
//out.println("saveFileName="+saveFileName);
boolean autoRefresh=false;
String autoRefreshString="";
String autoRefreshReceive=request.getParameter("autoRefresh");
if(autoRefreshReceive!=null ) {
autoRefresh=true;
autoRefreshString=" checked ";
//out.println("auto receive="+autoRefreshReceive);
}
int refreshSecond=3;
String refreshSecondReceive=request.getParameter("refreshSecond");
if(refreshSecondReceive!=null) {
refreshSecond=Integer.parseInt(refreshSecondReceive);
}
boolean viewAllThreadInfo=false;
String viewAllThreadInfoString="";
String viewAllThreadInfoReceive=request.getParameter("viewAllThreadInfo");
if(viewAllThreadInfoReceive!=null) {
viewAllThreadInfo=true;
viewAllThreadInfoString=" checked ";
}
boolean viewLockInfo=false;
String viewLockInfoString="";
String viewLockInfoReceive=request.getParameter("viewLockInfo");
if(viewLockInfoReceive!=null) {
viewLockInfo=true;
viewLockInfoString=" checked ";
}
int stackTraceNumber=100;
String stackTraceNumberReceive=request.getParameter("stackTraceNumber");
if(stackTraceNumberReceive!=null) {
stackTraceNumber=Integer.parseInt(stackTraceNumberReceive);
}
/************** request value check end ************************/
%>
<%=versionAndTitle%> by Sangmin, Lee |
|
|
|
|
<%
/*************************** Data Collect start *******************/
//long periodMillis=1000;
StringBuffer sb=new StringBuffer();
int blocked=0;
int newthread=0;
int runnable=0;
int terminated=0;
int timed_waiting=0;
int waiting=0;
//this attribute is used to change Lock Owner's color
HashSet lockOwnerSet=new HashSet();
sb.append("");
/********************* check if I can see the lock info ********************************/
if(viewLockInfo) {
sb.append("Ordinal | ")
.append("Thread Name | ")
.append("Thread State | ")
.append("Thread Time | ")
.append("Lock Owner | ")
.append("Stack Trace information |
");
} else {
sb.append("Ordinal | ")
.append("Thread Name | ")
.append("Thread State | ")
.append("Thread Time |
");
}
/********************* get ThreadMXBean info ********************************/
ThreadMXBean tmxBean=ManagementFactory.getThreadMXBean();
long threadList[]=tmxBean.getAllThreadIds();
HashMap stackTraceMap=new HashMap();
/********************* get Stack Traces infos start *******************/
// Because tempThreadInfo.getStackTrace() don't return anything, I use this way
if(viewLockInfo) {
Map stackInfos=null;
stackInfos=Thread.getAllStackTraces();
for (Thread tempThread: stackInfos.keySet()) {
stackTraceMap.put(tempThread.getId(),stackInfos.get(tempThread));
}
}
/********************* get Stack Traces infos end *******************/
try {
boolean blockFlag=false;
for(long id : threadList) {
blockFlag=false;
sb.append("");
long tempThreadTime=tmxBean.getThreadCpuTime(id);
ThreadInfo tempThreadInfo=tmxBean.getThreadInfo(id);
String threadName=tempThreadInfo.getThreadName();
State threadState=tempThreadInfo.getThreadState();//.getLockedSynchronizers();
if(threadState==State.BLOCKED) {blocked++;
blockFlag=true;
} else if(threadState==State.NEW) {newthread++;
} else if(threadState==State.RUNNABLE) {runnable++;
} else if(threadState==State.TERMINATED) {terminated++;
} else if(threadState==State.TIMED_WAITING) {timed_waiting++;
} else if(threadState==State.WAITING){ waiting++;
}
//MonitorInfo[] mis=tempThreadInfo.getLockedMonitors();
int tempOrdinal=threadState.ordinal();
if(viewAllThreadInfo) {
//sb.append("
Ordinal="+tempOrdinal+" "+threadName+"=>"+threadState +" ThreadTime="+tempThreadTime);
sb.append("");
sb.append(tempOrdinal);
sb.append(" | ");
sb.append(threadName);
sb.append(" | ");
if(blockFlag) {
sb.append("");
sb.append("");
sb.append(threadState);
sb.append("");
} else {
sb.append(" | ");
sb.append(threadState);
}
sb.append(" | ");
sb.append(getTimeString(tempThreadTime));
sb.append(" | ");
if(viewLockInfo) {
sb.append("");
String lockOwnerName=tempThreadInfo.getLockOwnerName();
if(lockOwnerName!=null) {
sb.append(lockOwnerName);
lockOwnerSet.add(lockOwnerName);
} else {
sb.append(" ");
}
sb.append(" | ");
sb.append("");
StackTraceElement[] tempStackTraceElement =stackTraceMap.get(id);
int stackTraceSize=tempStackTraceElement.length;
if(stackTraceSize!=0) {
if(stackTraceSize<=stackTraceNumber) {
for (StackTraceElement line: tempStackTraceElement) {
sb.append(line).append(" ");
}
} else{
for(int loop=0;loop");
}
}
} else {
sb.append(" ");
}
sb.append(" | ");
}
}
sb.append("
\n");
}
sb.append("
");
out.flush();
//Thread.sleep(periodMillis);
} catch(Exception e) {
e.printStackTrace();
}
/*************************** Data Collect end *******************/
%>
Total Started Thread Count |
<%=tmxBean.getTotalStartedThreadCount()%> |
Peak Thread Count |
<%=tmxBean.getPeakThreadCount()%> |
Current Thread Count |
<%=tmxBean.getThreadCount() %> |
Daemon Thread Count |
<%= tmxBean.getDaemonThreadCount() %> |
Thread State |
No. of state |
Description |
NEW |
<%=newthread %> |
A thread that has not yet started is in this state. |
RUNNABLE |
<%=runnable%> |
A thread executing in the Java virtual machine is in this state. |
BLOCKED |
<%=blocked%> |
A thread that is blocked waiting for a monitor lock is in this state. |
WAITING |
<%=waiting%> |
A thread that is waiting indefinitely for another thread to perform a particular action is in this state. |
TIMED_WAITING |
<%=timed_waiting%> |
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. |
TERMINATED |
<%=terminated%> |
A thread that has exited is in this state. |
<%
if(viewAllThreadInfo) {
if(lockOwnerSet.size()!=0) {
out.println("* Current Lock Owner List
");
out.println("");
for(String tempOwnerName:lockOwnerSet) {
out.println(" "+tempOwnerName+" | ");
}
out.println("
");
}
%>
* All Thread infos
<%
out.println(sb.toString());
}
%>
Copyright Sangmin, Lee. http://www.tuning-java.com All rights Reserved