站内搜索: 请输入搜索关键词

当前页面: 开发资料首页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

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select General. Under Projects, select Java Application. Click Next.
  2. In Project Name, type ProfilingThreads. Click Next.
  3. For Project Location, click Browse to select a project directory if you do not want to use the default project directory.
  4. Make sure the Set As Main Project option is checked.
  5. Make sure the Create Main Class option is not checked.
  6. 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.
  7. This final step is only required when NetBeans is configured to use JDK v5 as the default Java Platform.

    1. 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.
    2. 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.

  1. In the Projects window right-click the ProfilingThreads entry and choose New > Java Class.
  2. In Class Name, type DisplayForm.
  3. In Package, type profilingthreads.
  4. Click Finish.
  5. DisplayForm.java opens in the editor window. Delete all lines from DisplayForm.java and replace them with the contents of this DisplayForm.java file.
  6. Choose File > Save.
  7. 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. View Application Window

This is a simple Swing application that will display a message box when the requested time has elapsed.

  1. Make sure the Minutes Before Notification is set to 1.
  2. Click the Start! button. Notice how it does not redraw correctly.
  3. Click the Exit button. Notice how it does not respond at all.
  4. 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. View Incorrect Application Window
  5. After the minute eventually passes the message box will be displayed, as shown below. Click the OK button on the message box. View Notification Window
  6. The application window will begin to respond again. Click its Exit button to shut it down.

Profiling the Sample Program


  1. Choose Profile > Profile Main Project.
  2. If a dialog box is displayed asking for permission to modify the project build script, click OK.
  3. The Profile dialog is displayed. Click the large Monitor Application button.
  4. Make sure that Enable Threads Monitoring is checked.
  5. Click the Run button and the application will start. The IDE displays a Profiler Control Panel, as shown below.

    View Control Panel

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

    View Initial Threads Window

  7. 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.

  8. 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. View Initial Threads Window 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.
  9. 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.

  1. In the Projects window right-click the ProfilingThreads entry and choose New > Java Class.
  2. In Class Name, type SwingWorker.
  3. In Package, type profilingthreads.
  4. Click Finish.
  5. SwingWorker.java opens in the editor window. Delete all lines from SwingWorker.java and replace them with the contents of this SwingWorker.java file.
  6. Choose File > Save.
  7. 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.
  8. 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. View Code to Remove
  9. Choose File > Save.
  10. Choose Build > Build Main Project (or press F11).
  11. Choose Profile > Profile Main Project.
  12. If a dialog box is displayed asking for permission to modify the project build script, click OK.
  13. The Profile dialog is displayed. Click the large Monitor Application button.
  14. Make sure that Enable Threads Monitoring is checked.
  15. 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.
  16. When the sample program displays, click the Start! button. Notice how the Profiler's threads graph looks different this time; an example is below. View Threads Window w/SwingWorker 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.
  17. Click the Exit button on the sample program.
  18. 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. View Code to Remove 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.

Exploring Further


This tutorial has shown just one of the Profiler's many powerful features. Refer to the NetBeans Profiler help file for more information, or take a look at the introductory NetBeans IDE 4.0 Profiler Tutorial.

↑返回目录
前一篇: Visual Mobile Designer
后一篇: Wireless Connection Wizard