Ankit
Ankit

Reputation: 6980

Finding Source of Thread Creation in a Java application

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.

Upvotes: 18

Views: 5308

Answers (2)

Bananeweizen
Bananeweizen

Reputation: 22070

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.

Upvotes: 10

Ingo Kegel
Ingo Kegel

Reputation: 47975

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

Upvotes: 9

Related Questions