Last updated May 29, 2009 19:34, by Roman Ondruska
Feedicon  

Mulithreaded Debugging in NetBeans---Project Gallery

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

Have a look what this little app does...

  1. Start the application Gallery without debugger control (~ right-click the project node in the Projects window and choose Run).
  2. 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.
  3. Exit the application by closing the window.

Suspending Threads

Anytime when debugging, you can suspend/resume any thread...

  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' (Explorer group) where you can see that a new thread was created for each animation.
  3. Remove some animations (press button 'Less'), this 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 view. 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

You can do stepping in a selected thread leaving other threads unaffected...

  1. Use 'More'/'Less' button so that there are *two* animations running ('Thread_Martin', 'Thread_Dan').
  2. Go to project source code 'Gallery.java' and toggle line breakpoint at line number 174 ( currentImage = images[0]; ).
  3. Press button 'More' to create a new thread ('Thread_Tomas')--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. This feature is often essential when debugging synchronization issues.

Breakpoints and Multithreading

Now, there are three animations ('Thread_Martin', 'Thread_Dan', 'Thread_Tomas')--first two running (or suspended by 'Suspend Thread' button) and the third one suspended by the last 'Step Over'. Have a look what happens when a new thread hits breakpoint while debugging 'Thread_Tomas'...

  1. Press button 'More' again to create a new animation ('Thread_Jirka'). It will get suspended at the line breakpoint much like the previous thread, but there is one significant difference: The program counter (debugger green bar) in the editor still works for the previous thread (Thread_Tomas), 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 the debugger program counter will switch to the new breakpoint hit.
  3. Do 'Step Over' several times and watch the animation being performed by the new thread ('Thread_Jirka').

Switching Threads

When doing multithreaded debugging, switching among threads is often a common need. There are several ways to do it...

  1. Debugging 'Thread_Jirka', press Alt+Shift+T (Set current thread) to switch to the previous thread ('Thread_Tomas') and do several steps.
  2. Debugging 'Thread_Tomas', press Alt+S+T to swich back to 'Thread_Jirka'.
  3. Find 'Thread_Tomas suspended here' editor gutter annotation and via its context menu switch to 'Thread_Jirka' (Set Current Thread To > Thread_Jirka).
  4. Anytime, you can double-click a thread in the Debugging view to debug it; double-click 'Thread_Tomas' to check it out.

Debugging View Customization

Debugging view provides several customization icons allowing threads filtering and sorting. Most of these actions have self-explaining names, let's mention one of them here: If multithreaded debugging is not what you need at the moment, i.e. you are debugging the code running in just one thread, try to choose 'Show Less' mode. Only the current thread (~thread with the debugging counter) and all suspended threads will be displayed.

  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close