当前页面: 开发资料首页 → Netbeans 专题 → Watching Your Threads with the NetBeans IDE 4.0 Profiler
Watching Your Threads with the NetBeans IDE 4.0 Profiler
摘要: This article is intended to be used with the NetBeans Profiler, Milestone 6. If you are using Milestone 5 please use this version.
Creating a General project
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select General.
Under Projects, select Java Application. Click Next.
- In Project Name, type ProfilingThreads. Click Next.
- For Project Location, click Browse to select a project directory if you
do not want to use the default project directory.
- Make sure the Set As Main Project option is checked.
- Make sure the Create Main Class option is not checked.
- Click Finish.
The ProfilingThreads project opens in the
IDE. You can view its logical structure in the Projects window and its file
structure in the Files window.
- This final step is only required when NetBeans is configured to use JDK v5 as
the default Java Platform.
- The current Profiler only supports .class files created by
JDK v1.4.2, so if you have not installed a v1.4.2 JDK and configured it as a NetBeans
Java Platform, you will need to do so. Note that in its beta release the NetBeans Profiler installs
a JDK based on v1.4.2. For more information refer to the online
Profiler help.
- In the Projects window right-click the ProfilingThreads
entry and choose Properties. Select a Java Platform that uses the v.1.4.2 JDK and then
click the OK button.
Retrieving and Compiling the Source Code.
- In the Projects window right-click the ProfilingThreads
entry and choose New > Java Class.
- In Class Name, type DisplayForm.
- In Package, type profilingthreads.
- Click Finish.
- DisplayForm.java opens in the editor window. Delete all lines from
DisplayForm.java and replace them with the contents of
this DisplayForm.java file.
- Choose File > Save.
- Choose Build > Build Main Project (or press F11).
Running the Sample Program
Choose Run > Run Main Project (or press F6). A dialog box will be displayed
asking if you want to set
profilingthreads.DisplayForm as the main class.
Click OK and the program will display its window, shown below.

This is a simple Swing application that will display
a message box when the requested time has elapsed.
- Make sure the Minutes Before Notification is set to 1.
- Click the Start! button. Notice how it does not redraw correctly.
- Click the Exit button. Notice how it does not respond at all.
- Obstruct the view of the window by putting another window on top of part of it.
When you move the other window away, notice how the sample application does
not redraw its window correctly. An example is shown below.

- After the minute eventually passes the message box will
be displayed, as shown below. Click the OK button
on the message box.

- The application window will begin to respond
again. Click its Exit button to shut it down.
Profiling the Sample Program
- Choose Profile > Profile Main Project.
- If a dialog box is displayed asking for permission to modify the project build
script, click OK.
- The Profile dialog is displayed. Click the large Monitor Application button.
- Make sure that Enable Threads Monitoring is checked.
- Click the Run button and the application will start. The IDE
displays a Profiler Control Panel, as shown below.

- Click the Threads button or choose Profile > View > Threads. The Profiler will
display thread state in its main window; an example is below.

Color coding is used to display thread state.
- Green: the thread is either
running or is ready to run.
- Purple: the thread is sleeping, for example
it called Thread.sleep().
- Yellow: the thread is waiting in a call
to Object.wait().
- Red: the thread is blocked while trying to enter
a synchronized block or method.
- Click the Start! button on the sample application and watch what happens
to the thread labeled AWT-EventQueue-0. It will turn green and
stay that way for a full minute, as shown in the sample below.
This graph shows why the application is not responding.
The thread labeled AWT-EventQueue-0 is
the
Event Dispatch Thread (EDT) used by the
Swing library
to process window events. In a well behaved Swing
application, the EDT spends most of its time waiting and very little
time running; it should only
run for the brief amount of time required to dispatch an event. If the
event handlers in the application do not return quickly, however, then the program will
become unresponsive, just like in this sample.
- When the sample application is able to respond again, click its Exit button to shut it down.
Repairing the Sample Program
The solution to the problem is well known: move any
lengthy processing/waiting out of event handlers that run on the EDT.
One long time solution is the
SwingWorker class,
which is what will be used here.
- In the Projects window right-click the ProfilingThreads
entry and choose New > Java Class.
- In Class Name, type SwingWorker.
- In Package, type profilingthreads.
- Click Finish.
- SwingWorker.java opens in the editor window. Delete all lines from
SwingWorker.java and replace them with the contents of
this SwingWorker.java file.
- Choose File > Save.
- In the Editor window that contains DisplayForm.java, uncomment the block
of code that begins at line 132 and ends at line 157. Tip: If the editor is not
configured to display line numbers, line 130 is a comment that includes the
text "the right way," so you choose Edit > Find and search for that string.
- In the Editor window that contains DisplayForm.java, remove (or comment out) the block
of code that begins at line 120 and ends at line 128. It is shown highlighted
in the illustration below.

- Choose File > Save.
- Choose Build > Build Main Project (or press F11).
- Choose Profile > Profile Main Project.
- If a dialog box is displayed asking for permission to modify the project build
script, click OK.
- The Profile dialog is displayed. Click the large Monitor Application button.
- Make sure that Enable Threads Monitoring is checked.
- Click the Run button and the application will start. The IDE
displays the Profiler Control Panel. Click the Threads button or choose Profile > View > Threads.
- When the sample program displays, click the Start! button. Notice how
the Profiler's threads graph looks different this time; an example is below.
The EDT is yellow and the thread created by the application, which is
named "Our SwingWorker #1" is green. The EDT is not being used
to do a time consuming task and as a result
the buttons and other program controls remain responsive.
- Click the Exit button on the sample program.
- Right click the AWT-EventQueue-0 thread in the Profiler graph and
then select Thread Details. The Profiler displays a pie chart showing
time spent in each state; an example is below.
This graph can help you determine if your program is spending the correct
amount of time in each thread. The example shown is from after
the code was repaired so it shows that the EDT spent most of its
time waiting, which is exactly the behavior it should have.