Suspending and Resuming Threads from 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 procedures to suspend and resume the threads of any multithreaded application.

To suspend a thread

  1. Set a breakpoint in a statements that execute after your application’s threads have been created. In this procedure’s example, breakpoints are set in the Coffee class on the following statement:
         System.out.println("I Like Coffee" + " " + i);

— and —

in the Tea class on the following statement:

         System.out.println("I Like Tea" + " " + i);
  1. On the Debug menu, click Start, or press the keyboard shortcut, F5 to run your application and start the debugger.

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

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

  3. In the Threads debug window, highlight one of the threads. For this example, highlight Thread-1 of Tea.run.

  4. Click the right mouse button and select Suspend from the shortcut menu.

    Notice the Suspension Count for Thread-1 changes from 0 to 1.

  5. Continue to run your application with at least one of the threads suspended by pressing the F5 or F11 keys.

    If you are using the application created for this procedure, you’ll see that only the output from the non-suspended thread appears in JVIEW’s console window.

At some point in the debugging of a multithreaded application you may want to re-start the suspended thread. Follow these steps to resume a suspended thread:

Note   This procedure requires that you have:

To resume a suspended thread

  1. In the Threads debug window, highlight a suspended thread. For this example, highlight Thread-1 of Tea.run.

  2. Click the right mouse button and select Resume from the shortcut menu.

    Notice the Suspension Count for Thread-1 changes from 1 to 0.