如何找出谁在Java中创建线程? [英] how to find out who create a thread in java?

查看:50
本文介绍了如何找出谁在Java中创建线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在tomcat中,如果Web应用程序确实停止了none守护进程线程,则无法通过shutdown.sh关闭tomcat.

in tomcat,if a webapp did stop a none daemon thread,tomcat can not be shutdown by shutdown.sh

例如:

public class demo implements ServletContextListener{

  public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    // yes,we can cancel timer in here,but this is not the major problem
   }

  public void contextInitialized(ServletContextEvent arg0) {        
    Timer timer = new Timer();
    timer.schedule(new Test(), 1000, 1000*10);
    }
}

public class Test extends TimerTask{  
@Override
public void run() {
    System.out.println("AAAA");        
   }
}

如上所述,tomcat无法通过shutdown.sh关闭.来自jvisualvm,线程检查器说:

like as above,tomcat can not be shutdown by shutdown.sh. from jvisualvm,threads inspector say:

"Timer-0" - Thread t@40
   java.lang.Thread.State: TIMED_WAITING
    at java.lang.Object.wait(Native Method)
    - waiting on <3957edeb> (a java.util.TaskQueue)
    at java.util.TimerThread.mainLoop(Timer.java:552)
    at java.util.TimerThread.run(Timer.java:505)

   Locked ownable synchronizers:
    - None

堆栈信息未指出哪个Java类创建了线程,我的问题是如何找出谁从许多Web应用程序中创建了线程.

stack info did not point out which java class created the thread, my question is how to find out who created the thread from many webapps.

谢谢!

推荐答案

以下是方法列表,从最快/最可靠到最慢/最困难:

Here is the list of approaches, sorted from quickest / most reliable to slowest / hardest:

  1. 如果有该类的源,请在构造函数中创建一个异常(而不实际抛出该异常).您只需知道需要何时创建线程即可检查或打印它.

  1. If you have the source of the class, create an exception in the constructor (without actually throwing it). You can simply examine or print it when you need to know when the thread was created.

如果没有源代码,线程名称可能是创建它的一个很好的提示.

If you don't have the sources, the thread name can be a good hint who created it.

如果名称暗示通用服务(例如 java.util.Timer ),则可以在构造函数的IDE中创建条件断点.条件应为线程名称;当有人使用该名称创建线程时,调试器将停止.

If the name hints to a generic service (like java.util.Timer), then you can create a conditional breakpoint in your IDE in the constructor. The condition should be the thread name; the debugger will then stop when someone creates a thread with this name.

如果没有太多线程,请在 Thread 的构造函数中设置一个断点.

If you don't have too many threads, set a breakpoint in the constructors of Thread.

如果线程很多,请将调试器附加到应用程序并将其冻结.然后检查堆栈跟踪.

If you have many threads, attach a debugger to the app and freeze it. Then examine the stack traces.

如果其他所有操作失败,请获取 Java运行时源代码,并在其中添加日志记录代码.您要观察的类,编译一个新的 rt.jar 并将原始的替换为您的版本.请不要在生产中尝试此方法.

If everything else fails, get the source code for the Java runtime and add logging code in the classes you want to observe, compile a new rt.jar and replace the original one with your version. Don't try this in production, please.

如果钱不是问题,则可以使用动态跟踪工具,例如分别是SystemTap 和dtrace.

If money isn't an issue, you can use dynamic tracing tools like Compuware APM or, if you're on Linux or Solaris, you can try SystemTap and dtrace, respectively.

这篇关于如何找出谁在Java中创建线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆