18

I am working on a Java application which has a threading issue.

While using the applications for some time with Netbeans profiler attached, I can see several threads are created. Most of them finish in some .5 seconds. I could only find SwingWorkers used in the application.

Moreover, the majority of threads displayed by the profiler are normal threads and not SwingWorkers. Unless these threads were created by SwingWorker indirectly, I suspect, some library that the application uses is creating them.

Now I would like to remove this issue. But I could not find a way to determine the source of thread creation. If you can please suggest some other profiler/tool by means of which I can find the source(method) of thread creation.

Ankit
  • 6,772
  • 11
  • 48
  • 84
  • Have you considered grabbing a [current stack trace][1] in the constructor of your thread? [1]: http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java – OldCurmudgeon May 14 '12 at 09:39
  • @OldCurmudgeon As I just see the threads in the profiler's results, I do not know when and where are the threads created. Hence I do not think I can use it now. However, once I use the profilers listed below I might use it in some localized case, if required. Thanks for the link – Ankit May 14 '12 at 17:01
  • Related: http://stackoverflow.com/questions/9874641/tracking-java-thread-creation-and-lifetime – Aaron Digulla Nov 11 '16 at 09:43
  • The *JDK Flight Recorder* (formally *Java Flight Recorder*, freely available as part of OpenJDK since Java 11) should be able to capture stack traces for thread creation. – JojOatXGME Jun 01 '21 at 08:57

2 Answers2

9

If using Eclipse and its debugger is an option, you might try the following:

  • Import the code into a Java project.
  • Ctrl-Shift-T (Open Type), enter "Thread". The binary source editor for the Thread class opens.
  • Select all the Thread constructors in the Outline view, use context menu "Toggle Method Breakpoint". That creates breakpoints for the constructors.
  • Run and debug.

Alternatively

You could get the Yourkit Java profiler, which is also available for evaluation. It can show the threads created in an application including their stack traces (also after the thread finished). It does not show where the threads were created, but the stack trace of the threads might give you some clues about the involved libraries.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
  • 1
    Wow, that's a great answer, that's also teaching how to use Eclipse properly, and how useful it can be. – dantuch May 11 '12 at 18:04
9

JProfiler can do that. The thread monitor view shows the stack trace where a thread was created - if CPU recording was active at that time:

enter image description here

Disclaimer: My company develops JProfiler

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
  • Mhh... I just wanted to use this feature in *JProfiler 10.1.5* but the stack traces were not at all related to the location where the thread has been created. I got methods like `ClassLoader.loadClass(String)` or methods which are not doing anything with threads. Looks like this feature is kind of broken in JProfiler right now. :/ I finally found the right place by creating a breakpoint in `Thread.start` with an condition that checks for the name of the thread. – JojOatXGME May 31 '21 at 15:52
  • The displayed stack traces only show profiled classes. To show all classes, use sampling and disable all call tree filters in the profiling settings. – Ingo Kegel May 31 '21 at 16:25
  • I still get pretty random results which are not related to the location where the thread is created. If I use instrumentation instead of sampling, it seems to work, through. – JojOatXGME Jun 01 '21 at 07:42