Previous Topic

Next Topic

Book Contents

Book Index

Profiling the Deadlock Demo

The Deadlock demo represents a typical deadlock of threads over common object monitors, which can be successfully tracked down by using the features of profiler.

Starting the Deadlock Demo

To start the demo in profiling mode:

  1. Start Eclipse and import the demo project:
    1. In the Java perspective use File > Import from the main menu bar.
    2. In the Select dialog of the Import wizard select Existing Projects into Workspace and click Next.
    3. In the Import Project dialog, check Select root directory, use the Browse button and locate the directory <eclipse_home_dir>/dropins/mBProfiler/demo/ClassicDeadlock. The Projects list underneath will be populated with the entry for the demo's project.

    4. In the Projects list, select the demo checkbox and click Finish.

    On success, the demo project ClassicDeadlock demo will appear in the Package Explorer view.

  2. Go to Window >Preferences (for OS X use Eclipse >Preferences) and then to Java > Installed JREs to configure a default Java runtime that is supported by mBProfiler as described in the "List of Supported VMs, OSes and Architectures for Profiling" document.
  3. To run the demo in profiling mode:

    As a result, the Deadlock demo will be started based the predefined profile launch configuration.

  4. Next, select OK to allow Eclipse to open mBProfiler perspective with the application's profiling data shown. If you want to skip this step go to Window >Preferences (or Eclipse >Preferences if you are using OS X) and then to Run/Debug > Launching and set Save required dirty editors before launching to Always.

This quick startup will not include auto-start of CPU profiling – to have this profiling information gathered, use the button from the CPU Call Tree or CPU Hot Spots view.

You can also run the demo with additional settings such as a different JVM, command line arguments, etc. in the demo's profile launch configuration accessible with the Run > Profile Configurations menu command. Refer to the Starting Local Profiling guide for more details.

Profiling the Demo with mBProfiler

When the demo is started, its ClassicDeadlock instance creates two threads, LeftToRight and RightToLeft. During execution, each thread locks its own monitor and tries to do the same with the one of the other thread by means of a synchronized method. Hence, shortly after the startup, a deadlock emerges. During profiling, you can use mBProfiler to easily detect such thread conflicts.

First, let's inspect the state of the LeftToRight and RightToLeft threads visualized in the Threads view. There, it is indicated that the two threads after running awhile have blocked.

In case you have taken a snapshot of the thread monitors within the applications (mBProfiler > View Monitors Snapshot during profiling) , in the Monitors view you can also examine closely which are the conflict objects (painted in red) and the calls that have led to the deadlock.