Observing Thread Activity in the Threads Debug Window

   

The application example for this procedure creates two threads in the main() method and displays output to a JVIEW console window. This procedure refers to code created in the Multithreaded Beverages Application. If you’d like to create this project to complete the following exercises, do so now. You may also use the following procedure to observe thread activity of any multithreaded application.

To observe thread activity in the Threads debug window

  1. Set a breakpoint in the Coffee class on the following statement:
         System.out.println("I Like Coffee" + " " + i);
  1. Set a breakpoint in the Tea class on the following statement:
         System.out.println("I Like Tea" + " " + i);
  1. In Class1’s main() method, place the insertion point on the following statement:
      Coffee m_Coffee = new Coffee();  //creates Coffee object
  1. Click the right mouse button and select Run To Cursor from the shortcut menu.

  2. When your application enters break mode, open the Threads debug window.

    Notice the main() thread is currently the only thread running in this window.

  3. Press F5 to run to the breakpoint set in the Coffee class.

    More information appears in the Threads debug window to identify the names, location in the code, and suspension status for the application’s three threads, main, Thread-0, and Thread-1. The yellow arrow indicates which thread is currently running, in this case it is Thread-0 of Coffee.run(). Notice the main()method's thread is no longer running, because it has passed control to the threads of the Coffee and Tea classes.

    Note   Although this scenario suggests the Coffee thread will be the first thread to run, this may not always be the case. The operating system will ultimately determine the processing order of the threads in a given application unless there are statements in the code to specify conditions under which to create and run a new thread.

  4. Press F11 to execute the current statement.

  5. Switch to JVIEW’s console window and the following output appears:

    I Like Coffee 0

  6. Press F5 to run to the breakpoint set in the Tea class.

  7. Press F11 to execute the current statement and the following output appears in JVIEW’s console window:

    I Like Tea 0

  8. Continue pressing F5 and F11 alternately until one of the following output statements appears in JVIEW’s console window:

    I Like Coffee 9

    –or–

    I Like Tea 9

    Notice when a thread is destroyed information about that thread no longer appears in the Threads debug window.

In the process of observing each of your application’s threads, you may observe that a particular thread’s behavior is not what you had expected. To isolate a given thread by suspending other threads, see Suspending and Resuming Threads from the Threads Debug Window.