1、通過top找到對應的耗費資源比較大的進程ID,
2、ps p 進程ID -L -o pcpu,pid,tid,time,tname,cmd
3、然后利用上面面命令來找到對應的線程
4、利用Listthread方法 列出所有線程,與找到對應資源比較大的匹配
5、利用stack查找到對應的堆棧調用代碼,來分析到具體什么位置導致的
public void ListThread()
?? ?{?
?? ?String pid=?? ?querytopCPU();?
?? ? ?String cmd2="jstack -l "+pid;
?? ??? ?Map<String,String> mssrg=nid(cmd2);
?? ??? ?ThreadGroup group = Thread.currentThread().getThreadGroup(); ?
?? ??? ?ThreadGroup topGroup = group; ?
?? ??? ?// 遍歷線程組樹,獲取根線程組 ?
?? ??? ?while (group != null) { ?
?? ??? ? ? ?topGroup = group; ?
?? ??? ? ? ?group = group.getParent(); ?
?? ??? ?} ?
?? ??? ?// 激活的線程數加倍 ?
?? ??? ?int estimatedSize = topGroup.activeCount() * 2; ?
?? ??? ?Thread[] slackList = new Thread[estimatedSize]; ?
?? ??? ?// 獲取根線程組的所有線程 ?
?? ??? ?int actualSize = topGroup.enumerate(slackList); ?
?? ??? ?// copy into a list that is the exact size ?
?? ??? ?Thread[] list = new Thread[actualSize]; ??
?? ??? ?java.lang.System.arraycopy(slackList, 0, list, 0, actualSize); ?
?? ??? ?java.lang.System.out.println("Thread list size == " + list.length); ?
?? ??? ?for (Thread thread : list) { ?
?? ??? ??? ?java.lang.System.out.println(thread.getName()); ?
?? ??? ?} ??
?? ??? ?
?? ??? ?int i=0;?
?? ??? ?List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>(); ?
?? ??? ?for(Thread t :?? ? list){
?? ??? ?Map<String, Object>?? ?imap = new HashMap<String, Object>();
?? ??? ? ? ?imap.put("id", t.getId());
?? ??? ??? ?imap.put("name", t.getName());
?? ??? ??? ?imap.put("state", t.getState());
?? ??? ??? ?imap.put("whofirst",t.getPriority());
?? ??? ??? ?imap.put("groupname",t.getThreadGroup().getName());
?? ??? ??? ?imap.put("pid",pid);
?? ??? ??? ?String tid="";
?? ??? ??? ?
?? ? ? ? ?? ?for (Entry<String, String> entry : mssrg.entrySet()) {
?? ? ? ? ??? ??? ?
? ? ?? ??? ??? ?String key = (String) entry.getKey();
? ? ?? ??? ??? ?String value = (String) entry.getValue();
? ? ?? ??? ??? ?if(value.indexOf(t.getName())>-1)
? ? ?? ??? ??? ?{?
? ? ?? ??? ??? ??? ?tid=key;
? ? ?? ??? ??? ?}
? ? ?? ??? ??
? ? ? ?? ?}
?? ??? ??? ?//
?? ??? ??? ?
?? ??? ??? ?imap.put("tid",tid);
?? ??? ??? ?String stname="";
?? ??? ??? ?if(t.getStackTrace().length>0)
?? ??? ??? ?{
? ? ? ? ? ? ? ? stname+="ClassName:"+ste.getClassName()+",FileName:"+ste.getFileName()+",Method:"+ste.getMethodName()+",LineNum:"+ste.getLineNumber();
//?? ??? ??? ??? ?}
?? ??? ??? ??? ?stname=t.getStackTrace()[t.getStackTrace().length-1].getFileName()+",LineNum:"+t.getStackTrace()[t.getStackTrace().length-1].getLineNumber();
?? ??? ??? ?}
?? ??? ??? ?imap.put("stack",stname);
?? ??? ??? ?list2.add(imap);
?? ??
?? ??? ?}?
?? ??? ?// 將查詢的數據返回到前臺
?? ??? ?sendMsg(JSONTool.convertCollection2Json(list2));?? ?
?? ?}
?
?
public String querytopCPU()
?? ?{
?? ??? ?String name = java.lang.management.ManagementFactory.getRuntimeMXBean().getName(); ? ?
?? ??? ?java.lang.System.out.println(name); ? ?
?? ??? ?// 首先獲取本應用程序的PID
?? ??? ?String pid = name.split("@")[0]; ? ?
?? ??? ?java.lang.System.out.println("Pid is:" + pid); ??
?? ??? ? ?//windows ?
//?? ? ? ? ?String cmd = "F:\\apache-tomcat-6.0.20.exe"; ?
//?? ? ? ? ?String cmd = "D:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE F:\\test.doc"; ?
//?? ? ? ? ?String cmd = "cmd.exe /c start F:\\test.doc"; ?
?? ? ? ? ? ?String cmd = "ping www.baidu.com"; ?
?? ? ? ? ? ?//linux ?
//?? ? ? ? ?String cmd = "./fork_wait"; ?
//?? ? ? ? ?String cmd = "ls -l"; ?
//?? ? ? ? ?String[] cmd=new String[3]; ?
//?? ? ? ? ?cmd[0]="/bin/sh"; ?
//?? ? ? ? ?cmd[1]="-c"; ?
//?? ? ? ? ?cmd[2]="ls -l ./"; ? ?
?? ? ? ? //獲取線程列表
?? ? ? ? String cmd1="ps p "+pid+" -L -o pcpu,pid,tid,time,tname,cmd";
?? ? ? ? String cmd2="jstack -l "+pid;
?? ? ? ? String cmd3="ps p "+pid+" -L -o tid,pcpu";
?
?? ? ? ? return pid;
?? ? ? ?}?
?
public String stack(@RequestParam(value = "id", required = false) String id)
?? ?{?
String rtStr="";
?? ??? ?ThreadGroup group = Thread.currentThread().getThreadGroup(); ?
?? ??? ?ThreadGroup topGroup = group; ?
?? ??? ?// 遍歷線程組樹,獲取根線程組 ?
?? ??? ?while (group != null) { ?
?? ??? ? ? ?topGroup = group; ?
?? ??? ? ? ?group = group.getParent(); ?
?? ??? ?} ?
?? ??? ?// 激活的線程數加倍 ?
?? ??? ?int estimatedSize = topGroup.activeCount() * 2; ?
?? ??? ?Thread[] slackList = new Thread[estimatedSize]; ?
?? ??? ?// 獲取根線程組的所有線程 ?
?? ??? ?int actualSize = topGroup.enumerate(slackList); ?
?? ??? ?// copy into a list that is the exact size ?
?? ??? ?Thread[] list = new Thread[actualSize]; ??
?? ??? ?java.lang.System.arraycopy(slackList, 0, list, 0, actualSize); ?
?? ??? ?java.lang.System.out.println("Thread list size == " + list.length); ?
?? ??? ?for (Thread thread : list) { ?
?? ??? ??? ?java.lang.System.out.println(thread.getName()); ?
?? ??? ??? ?String tid=thread.getId()+"";
?? ??? ??? ?if(tid.equals(id) )
?? ??? ??? ?{
?? ??? ??? ??? ?for(StackTraceElement ste : thread.getStackTrace())
?? ??? ??? ??? ??? ?{?
?? ??? ??? ??? ??? ?rtStr+="ClassName:"+ste.getClassName()+",FileName:"+ste.getFileName()+",Method:"+ste.getMethodName()+",LineNum:"+ste.getLineNumber()+"\r\n";
?? ??? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?} ??
?? ??? ?return rtStr;
?? ?}