Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 716
Ankit Avatar asked May 11 '12 17:05

Ankit


People also ask

Where are threads created in Java?

All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program's start, when the main() method is invoked. In Java, creating a thread is accomplished by implementing an interface and extending a class.

Where is a thread created?

A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called. The Main thread in Java is the one that begins executing when the program starts.

How is a thread created in Java explain with example?

You can create threads by implementing the runnable interface and overriding the run() method. Then, you can create a thread object and call the start() method. Thread Class: The Thread class provides constructors and methods for creating and operating on threads.


2 Answers

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.

like image 159
Bananeweizen Avatar answered Nov 16 '22 01:11

Bananeweizen


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

like image 31
Ingo Kegel Avatar answered Nov 16 '22 01:11

Ingo Kegel