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.

4
  • Have you considered grabbing a [current stack trace][1] in the constructor of your thread? [1]: stackoverflow.com/questions/1069066/… Commented May 14, 2012 at 9: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 Commented May 14, 2012 at 17:01
  • Related: stackoverflow.com/questions/9874641/… Commented Nov 11, 2016 at 9: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. Commented Jun 1, 2021 at 8:57

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, that's a great answer, that's also teaching how to use Eclipse properly, and how useful it can be.
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

3 Comments

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.
The displayed stack traces only show profiled classes. To show all classes, use sampling and disable all call tree filters in the profiling settings.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.