java怎么看谁创建了线程,如何找出谁在Java中创建线程?

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

for example:

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");

}

}

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

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.

Thanks!

解决方案

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

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.

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.

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.

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值