日期:2014-05-19 浏览次数:20876 次
import org.slf4j.Logger;
public class TraceUtil {
final Logger logger;
final long threshold = 1000;
private long begin;
private long offtime = 0;
private String threadInfo;
private String targetId;
public TraceUtil(Logger logger, Thread thread, String targetId, long begin) {
this.logger = logger;
this.threadInfo = thread.getId() + "-" + thread.toString();
this.targetId = targetId;
this.begin = begin;
}
public void trace(String targetEvent) {
long duration = System.currentTimeMillis() - begin;
long increment = duration - offtime;
offtime = duration;
float percentage = (float) increment / (float) duration * 100;
if (duration > threshold && percentage > 20) {
logger.error(
"Response time is too large: [{}], {}/{} ({}), {}, {}",
new String[] { threadInfo + "", increment + "",
duration + "", percentage + "%", targetEvent,
targetId });
}
}
}
<%@ page import="java.lang.management.*, java.util.*" %>
<%!
Map cpuTimes = new HashMap();
Map cpuTimeFetch = new HashMap();
%>
<%
out.println("Threads Monitoring");
long cpus = Runtime.getRuntime().availableProcessors();
ThreadMXBean threads = ManagementFactory.getThreadMXBean();
threads.setThreadContentionMonitoringEnabled(true);
long now = System.currentTimeMillis();
ThreadInfo[] t = threads.dumpAllThreads(false, false);
for (int i = 0; i < t.length; i++) {
long id = t[i].getThreadId();
Long idObj = new Long(id);
long curre