[Java] java 查看web应用中的阻塞线程 →→→→→进入此内容的聊天室

来自 , 2020-12-06, 写在 Java, 查看 161 次.
URL http://www.code666.cn/view/bdb106a0
  1. <%@page import="org.apache.jasper.tagplugins.jstl.core.Out"%>
  2. <%@page import="java.util.HashMap"%>
  3. <%@page import="java.util.Map"%>
  4. <%@page import="java.lang.management.ThreadInfo"%>
  5. <%@page import="java.lang.management.ThreadMXBean"%>
  6. <%@page import="java.lang.management.ManagementFactory"%>
  7. <%@ page language="java" pageEncoding="UTF-8"%>
  8. <%@ taglib prefix="c" uri="/WEB-INF/c.tld"%>
  9. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13.     <title>Threads in ldcstudy.com</title>
  14.     <style>
  15.         body
  16.         {
  17.             font-size: 8pt;
  18.         }
  19.  
  20.         ol
  21.         {
  22.             line-height: 18px;
  23.         }
  24.     </style>
  25. </head>
  26. <body>
  27.     <strong>java.io.tmpdir:</strong>
  28.     <ul>
  29.         <li><%=System.getProperty("java.io.tmpdir")%></li>
  30.     </ul>
  31.     <br />
  32.     <strong>Memory:</strong>
  33.     <ol>
  34.         <li>freeMemory=<%=Runtime.getRuntime().freeMemory()/(1024*1024)%>M</li>
  35.         <li>totalMemory=<%=Runtime.getRuntime().totalMemory()/(1024*1024)%>M</li>
  36.         <li>maxMemory=<%=Runtime.getRuntime().maxMemory()/(1024*1024)%>M</li>
  37.     </ol>
  38.     <br />
  39.     <strong>Thread:</strong>
  40.     <ol>
  41.         <%
  42. for(Thread t : list_threads()){%>
  43.         <li><%=t.getName()%>(<b><%=t.getState()%></b>)[<%=timemap.get(t.getId()) %>] : <%=t.getClass().getName()%></li>
  44.         <%}%>
  45.     </ol>
  46.     <%!
  47. public static java.util.List<thread> list_threads(){
  48.     int tc = Thread.activeCount();
  49.     Thread[] ts = new Thread[tc];
  50.     Thread.enumerate(ts);
  51.     return java.util.Arrays.asList(ts);
  52. }
  53.  
  54. public static Map<Long, Long> cputime(){
  55.     ThreadMXBean tm = ManagementFactory.getThreadMXBean();
  56.     tm.setThreadContentionMonitoringEnabled(true);
  57.     long [] tid = tm.getAllThreadIds();
  58.     ThreadInfo [] tia = tm.getThreadInfo(tid, Integer.MAX_VALUE);
  59.  
  60.     long [][] threadArray = new long[tia.length][2];
  61.  
  62.     Map<Long, Long> map = new HashMap<Long, Long>();
  63.  
  64.     for (int i = 0; i < tia.length; i++) {
  65.         long threadId = tia[i].getThreadId();
  66.  
  67.         long cpuTime = tm.getThreadCpuTime(tia[i].getThreadId())/(1000*1000*1000);
  68.         //threadArray[i][0] = threadId;
  69.         //threadArray[i][1] = cpuTime;
  70.         map.put(threadId, cpuTime);
  71.     }
  72.     return map;
  73. }
  74.  
  75. Map<Long, Long> timemap = cputime();
  76.  
  77. Map cpuTimes = new HashMap();          
  78. Map cpuTimeFetch = new HashMap();    
  79. %>
  80. <hr/>
  81.  
  82. <%
  83.  
  84. long cpus = Runtime.getRuntime().availableProcessors();      
  85. ThreadMXBean threads = ManagementFactory.getThreadMXBean();      
  86. threads.setThreadContentionMonitoringEnabled(true);      
  87. long now = System.currentTimeMillis();      
  88. ThreadInfo[] t = threads.dumpAllThreads(false, false);
  89. out.print("t.length...." + t.length);
  90. int blockCount = 0;
  91. for (int i = 0; i < t.length; i++) {          
  92.     long id = t[i].getThreadId();          
  93.     Long idObj = new Long(id);          
  94.     long current = 0;          
  95.     if (cpuTimes.get(idObj) != null) {              
  96.         long prev = ((Long) cpuTimes.get(idObj)).longValue();              
  97.         current = threads.getThreadCpuTime(t[i].getThreadId());              
  98.         long catchTime = ((Long) cpuTimes.get(idObj)).longValue();              
  99.         double percent = (double)(current - prev) / (double)((now - catchTime) * cpus * 1000);    
  100.         out.print("<li>" + t[i].getThreadName()+ ":" + t[i].getBlockedTime() + ":" + percent + " : " + prev + "</li>");
  101.         if (percent > 0 && prev > 0) {
  102.  
  103.             out.println("<li>" + t[i].getThreadName()+"#"+t[i].getThreadId() + " Time: " + percent + " (" + prev + ", " + current + ") ");      
  104.             String locked = t[i].getLockInfo()==null?"":t[i].getLockInfo().getClass().toString();      
  105.             out.println(" Blocked: (" + t[i].getBlockedTime() + ", " + t[i].getBlockedCount() + ", " + locked + ")</li>");
  106.             StackTraceElement[] te = t[i].getStackTrace();
  107.             out.println("<ul>");
  108.             for(int j = 0; j < te.length; j++){
  109.                 out.println("<li>" + te[j].getClassName()+"#"+te[j].getMethodName() + " (LineNumber: " + te[j].getLineNumber() +  ") ");      
  110.             }
  111.             out.println("</ul>");
  112.             blockCount ++;
  113.         }          
  114.     }                    
  115.     cpuTimes.put(idObj, new Long(current));
  116.     cpuTimes.put(idObj, new Long(now));
  117. }
  118. out.print("<li>总阻塞线程:" + blockCount  +" 个</li>");
  119. %>
  120. </body>
  121. </html>
  122.  
  123. //源代码片段来自云代码http://yuncode.net
  124.                        

回复 "java 查看web应用中的阻塞线程"

这儿你可以回复上面这条便签

captcha