Mulithreaded Debugging in NetBeans---The Gallery Demo
Description
The Gallery demo is a simple Java Swing Application that plays animated images of NB debugger team members. The application has two buttons that enable to add and remove animated images--each image is animated by an extra thread. In this exercise we demonstrate NB multithreaded debugging capabilities including suspending threads, stepping in a selected thread, or handling breakpoints in multithreaded environment.
Requirements
NB 6.5 or better
Intro
Start the application Gallery without debugger control (~ right-click the project node in the Projects window and choose Run). Click the button 'More' several times to add some animated images and then click the button 'Less' to remove images one by one. Let's note again that each image is animated by an extra thread. Exit the application by closing the window.
Suspending Threads
1) Start debugging of the application Gallery (~ right-click the Gallery project in the Projects window and choose Debug). 2) Create some animated images (press button 'More') and have a look into 'Debugging view' (in Explorer group) where you can see that a new thread was created for each animation. 3) Remove some animations (press button 'Less'), you will also stop the corresponding threads--the thread will disappear from the 'Debugging view'. 4) Suspend/resume any thread (animation) by clicking the 'Suspend thread' button to the right of the thread in the Debugging window. When a thread is suspended, the icon for the thread changes to indicate the new state. You can expand the suspended thread node to view its call stack.
Stepping in Thread
1) Use 'More'/'Less' button so that there are *two* animations running. 2) Go to project source code 'Gallery.java' and toggle line breakpoint at line number 175 ( currentImage = images[0]; ). 3) Press button 'More' to create a new thread--the thread will get stopped at the line breakpoint. 4) Do 'Step Over' (F8) several times and watch the corresponding animation; image will appear after few steps and then you will be debugging the animation. Note that other animations (threads) are not effected by stepping, i.e. each other thread can be left running or suspended upon current debugging needs.
Breakpoints and Multithreading
1) Now, there are three animations (threads)--two of them running (or suspended by 'Suspend Thread' button) and the third one suspended by the last 'Step Over'. Let's press button 'More' again to create a new animation (thread). It will get suspended at the line breakpoint much like the previous thread, but there is a significant difference: The program counter (debugger green bar) in the editor still works for the previous thread, so you can finish your debugging of this thread--try several additional steps. Your are notified about the new thread breakpoint hit by the non-modal yellow panel in the 'Debugging view'. 2) Click the button on the yellow notification panel and debugger program counter will switch to the new breakpoint hit. 3) Do 'Step Over' several times and watch the animation being created by the new thread.





