Published 26 Oct, 2022

Java - How to find where a thread was originally started

Category Java
Modified : Dec 01, 2022
81

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?

Answers

There are 3 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Multithreading, Java, Debugging, Eclipse. These have been categorized in sections for a clear and precise explanation.

60

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.


42

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


40

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.