GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||
GeezerVison is showing netbeans.apache.org — Debugging Multi-threaded Applications in NetBeans IDE Debugging Multi-threaded Applications in NetBeans IDE
This document describes how to use the Debugging window in NetBeans IDE to debug multi-threaded applications. The tutorial also demonstrate how to use the IDE to detect deadlocks in an application. The Debugging window simplifies the debugging process by integrating into one window the information about debugging sessions, application threads and thread call stacks. The Debugging window enables you to easily see the status of application threads and suspend and resume any of the threads in the session. This tutorial uses two sample projects to demonstrate how to work with the Debugging window. To complete this tutorial you first download and open the projects Gallery and Deadlock.
Downloading the Sample ProjectsYou can download the sample projects used in this tutorial in the following ways.
When you click Finish, the IDE initializes the local folder as a Subversion repository and checks out the project sources.
Note. For more about checking out sources with Subversion, see the section on Setting up Subversion in the Guide to Subversion in NetBeans IDE. Opening the ProjectsIn this tutorial you will use two applications to demonstrate the IDE’s support for debugging multi-threaded applications. In this exercise you will open and then run the two projects in the IDE. After you run the projects you will then proceed to debug each of the projects. Running the Gallery ProjectThe Gallery application is a simple Java Swing application that plays animated images. The application has two buttons that enable you to add and remove animated images. In this exercise you run the Gallery application.
When you click Open the IDE opens and displays the project in the Projects window. If you expand the project node in the Projects window you can see that the project is a simple Java Swing application.
Figure 1. Gallery application
The Gallery project is a simple multi-threaded application that you will debug in this tutorial. Running the Deadlock ProjectThe Deadlock application contains a
When you click Open the IDE opens and displays the project in the Projects window. If you expand the project node in the Projects window you can see that the project is a simple Java application.
When you click Run, the Output window opens and displays the following output.
When the Deadlock application ends you will see the following in the Output window.
The Deadlock project is a simple Java application with two threads. When you debug the application you will create a deadlock to illustrate how the IDE can help you detect deadlocks. Debugging the Sample ProjectsThe Gallery project is a simple Java Swing application that displays animated images. You add and remove the images by clicking buttons in the application. Clicking the 'More' button starts a new thread that displays and animates an image. Clicking the 'Less' button stops the most recent thread, stopping the animation and removing the image. Suspending ThreadsIn this exercise you start debugging the Gallery application and add some images to start some application threads. When you start a debugging session the IDE opens the Debugging window in the left pane of the IDE. The Debugging window displays a list of the threads in the session.
When you click Debug, the IDE starts the Gallery application and opens the default debugging windows. The IDE automatically opens the Debugging window in the left side of the main window and opens the Debugger Console in the Output window.
If you look in the Debugging window you can see that a new thread was started for each animation.
Figure 2. Debugging window
When a thread is suspended, the icon for the thread changes to indicate the new state. You can expand the thread node to view the thread’s call stack. You can right-click items in the Debugging window to open a pop-up menu with debug commands.
Figure 3. Debugging window with two suspended threads
If you look at the Gallery application you can see that when you suspended the threads the animation for those threads stopped. The Debugging window enables you to quickly view and change the status of threads in the session. By default the Debugging window displays the Resume and Suspend buttons in the right side of the window. You can hide the buttons and further customize the display of the Debugging window by using the toolbar at the bottom of the Debugging window. If you are running multiple debugging sessions you can use the drop down list at the top of the Debugging window to choose which session is displayed in the window.
Figure 4. Debugging window toolbar
Switching ThreadsThis exercise demonstrates what happens when you are stepping through an application and a different application thread hits a breakpoint. In this exercise you will set a method breakpoint and start stepping through the application. While you are stepping through the application you will start a new thread that will also hit the breakpoint. The IDE informs you when this occurs by displaying a notification in the Debugging window. You will then switch between threads.
You can see that the Program Counter in the margin of the editor indicates your position as you step through the method.
When the new thread hits the method breakpoint a New Breakpoint Hit notification appears in the Debugging window that informs you that another thread hit a breakpoint while you were stepping through the method.
Figure 5. New Breakpoint Hit notification
When you are stepping through a thread and a breakpoint is hit in another thread, the IDE gives you the option to switch to the other thread or continue stepping through the current thread. You can click the arrow button in the New Breakpoint Hit notification to switch to the thread that encountered the breakpoint. You can switch to the new thread at any time by selecting the thread in the notice window. Stepping through the current breakpoint thread resumes the current thread but the status of other application threads remains unchanged. Note. If you look in the Debugging window you can see that the current thread (Thread_Jirka) is indicated by a green bar in the margin. The thread that invoked the notification by hitting the breakpoint (Thread_Roman) is indicated by a yellow bar and the thread icon indicates that the thread is suspended by a breakpoint.
Figure 6. New Breakpoint Hit notification
When you switch to the new thread you can see the following:
Figure 7. Editor showing debugging annotations
Figure 8. Editor showing Set as Current Thread pop-up
Alternatively, you can invoke the Current Thread Chooser (Alt+Shift+T; Ctrl+Shift+T on Mac) and switch to any of the application threads.
Figure 9. Gallery application
When you switch back to Thread_Jirka, the suspended thread annotation appears next to the line where Thread_Roman was suspended. You can resume Thread_Roman by clicking Resume in the Debugging window.
Figure 10. Editor showing debugging annotations
The Debugging window enables you to very precisely view and control thread states. The debugger manages application threads to simplify the debugging workflow and to prevent the debugging process from creating deadlocks. In this exercise you saw the following behavior when debugging an application in the IDE.
You can quit the Gallery application. In the next exercise you will debug the Deadlock application and use the IDE to help you detect a deadlock. Detecting DeadlocksThe IDE can help you identify potential deadlock situations by automatically searching for deadlocks among all suspended threads. When a deadlock is detected, the IDE displays a notification in the Debugging window and identifies the involved threads. To demonstrate the IDE’s deadlock detection, you will run the sample Deadlock project in the debugger and create a deadlock situation.
To set the breakpoint, click in the margin of the source editor next to the line where you want to set the breakpoint. The breakpoint annotation appears in the left margin next to the line. If you open the Breakpoints window (Alt-Shift-5; Ctrl+Shift+5 on Mac) you can see that the two breakpoints are set and enabled.
Figure 11. Editor showing breakpoint set at line 20
The
Figure 12. Resuming suspended threads in the Debugging window
Resuming the threads
Figure 13. Resuming suspended threads in the Debugging window
If you check the application for deadlocks and a deadlock is detected, a message appears in the Debugging Window informing you about the deadlock. You can see that the threads in deadlock are indicated with a red bar in the left margin of the Debugging window. This tutorial was a basic introduction to some of the debugging features in the IDE. The Debugging window enables you to easily suspend and resume threads when debugging an application. This can be extremely helpful when you are debugging multi-threaded applications. | ||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |