检测谁创建一个线程(w。Eclipse) [英] Detect Who Created a Thread (w. Eclipse)

查看:109
本文介绍了检测谁创建一个线程(w。Eclipse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何知道谁在Java中创建了一个线程?



想象一下:您在复杂的插件环境中使用〜30 第三方 JAR。你启动它,运行大量的代码,做一些计算,最后调用shutdown()。



此生命周期通常工作正常,除了每次运行一些(非守护进程)线程保持悬空。这将是没有问题,如果每次关机是最后一次关机,我可以简单地运行System.exit()在这种情况下。但是,这个循环可能会运行几次,每次通过都会产生更多的垃圾。



那么我该怎么办?我在Eclipse的Debug View中看到了线程。我看到他们的堆栈痕迹,但它们不包含任何关于它们的起源的提示。没有创建者的堆栈跟踪,没有可区分的类名,没有。有没有人有一个想法如何解决这个问题?/ p>

解决方案

好的,我能够自己解决(排序)问题:我把一个断点放到

  Thread.start()

并手动逐步调用每个调用。这样我很快就发现,这个Class.forName()初始化了大量的静态代码,作为回报,创建了这些神秘的线程。



虽然我能够解决我的问题,我仍然认为更一般的任务仍然没有解决。


How can I find out who created a Thread in Java?

Imagine the following: You use ~30 third party JARs in a complex plugin environment. You start it up, run lots of code, do some calculations and finally call shutdown().

This life-cycle usually works fine, except that on every run some (non-daemonic) threads remain dangling. This would be no problem if every shutdown was the last shutdown, I could simply run System.exit() in that case. However, this cycle may run several times and it's producing more garbage every pass.

So, what should I do? I see the threads in Eclipse's Debug View. I see their stack traces, but they don't contain any hint about their origin. No creator's stack trace, no distinguishable class name, nothing.

Does anyone have an idea how to address this problem?

解决方案

Okay, I was able to solve (sort of) the problem on my own: I put a breakpoint into

Thread.start() 

and manually stepped through each invocation. This way I found out pretty quickly that Class.forName() initialized lot of static code which in return created these mysterious threads.

While I was able to solve my problem I still think the more general task still remains unaddressed.

这篇关于检测谁创建一个线程(w。Eclipse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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