Michael Dietrich
Michael Dietrich

Reputation: 465

How to find where a thread was originally started

Supposed I have an application that can spawn multiple threads if needed for doing tasks ... so nothing special. I use Eclipse to write and debug Java applications. A thread (lets call it "async task") is immediatly respawned after it leaves the run() method (so there is bug and I want to find the reason for this behavior).

My question, if I pause this thread "async task" using the eclipse IDE (debug perspective ..) is there way to find out where this thread was originally started (for example using the Debug view or any other)? Because I want to know who spawns this thread (without making a text search or something like this).

Is there a good way to get this information?

Upvotes: 6

Views: 3117

Answers (4)

Sam
Sam

Reputation: 2039

In the debugger call stack, go all the way to "Thread.run" and see what the thread name is (this.name). Hopefully it is meaningful, and you can search your classpath for that common prefix. E.g. if your thread name is "special-task-98", the search for "special-task".

Upvotes: 0

Richard
Richard

Reputation: 21

Had a similar problem in production and wrote a litte java agent that logs the stack of every thread start. So the system can run and you get the info live in the log. That helps a lot, when you have many threads. https://github.com/xdev-software/thread-origin-agent

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You can't check whether new thread start or not by using debuger since debug will hang your entire JVM.

You can put some logs and check how threads works there.

Upvotes: -1

René Link
René Link

Reputation: 51353

I would set a breakpoint at Thread.start() and enable a condition

enter image description here

Whenever a thread named "async task" is started the condition is evaluated to true and the thread that invokes the start method is paused. Then you can see in the stacktrace from where the call came.

Upvotes: 10

Related Questions