在run方法中,如何查找从何处开始? [英] In run method, how to find from where start was called?
本文介绍了在run方法中,如何查找从何处开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在调试一些代码.我的调试器显示代码的来源是Thread.run().我需要知道从哪一部分代码Thread.start()调用了!有办法找出答案吗?
I am debugging some code. My debugger shows that the origin of code is from Thread.run(). I need to know from which part of code Thread.start() was called! Is there a way to find this out?
推荐答案
您可以使用new Throwable().getStackTrace()
获取完整的堆栈跟踪.要获取start
堆栈,您必须扩展Thread
,这是实际上有必要进行的几次扩展(最好使用Runnable
).
You can use new Throwable().getStackTrace()
to get a full stack trace. To get the start
stack you must extend Thread
, one of the few times it is actually necessary to do so (mostly using Runnable
is preferred).
class C extends Thread {
StackTraceElement[] constructed;
StackTraceElement[] started;
public C() {
constructed = new Throwable().getStackTrace();
}
@Override
public void run() {
// Your suuff.
}
@Override
public synchronized void start() {
started = new Throwable().getStackTrace();
super.start();
}
}
这篇关于在run方法中,如何查找从何处开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文