JVM TI学习----如何中断weblogic中stuck thread
2009-09-22 00:00:00 来源:WEB开发网有了线程名、connection,我们就可以用下面的方法去中断挂死线程了,
1 public void terminateThread(String threadPattern){
2 if(vm != null){
3 List threads = vm.allThreads();
4 ThreadReference tr = null;
5 int loop = -1;
6 for(loop=0; loop<threads.size(); loop++){
7 if(tr.toString().indexOf(threadPattern) != -1){
8 tr.interrupt();
9 System.out.println(threadPattern + " is terminated!");
10 break;
11 }
12 }
13 if(loop == threads.size())
14 System.out.println("no matched thread was found in target VM!");
15 }
16 }
线程被中断的时候,会收到InterruptedExcetpion,比如我在测试中让线程sleep,然后利用上面的程序去中断sleep,收到的异常如下:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at test.jpda.clazz.Test2.run(Test2.java:14)
at test.jpda.clazz.Test1.run(Test1.java:10)
at test.jpda.clazz.Test.run(Test.java:9)
at jsp_servlet.__index._jspService(__index.java:91)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.ru(WebAppServletContext.java:3498)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
注意:线程名一定不能写错啊,否则就可能误杀了,老板批你别怨俺。
更多精彩
赞助商链接