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
System.out.println("I Like Coffee" + " " + i);
System.out.println("I Like Tea" + " " + i);
main()
method, place the insertion point on the following statement: Coffee m_Coffee = new Coffee(); //creates Coffee object
Notice the main()
thread is currently the only thread running in this window.
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.
I Like Coffee 0
I Like Tea 0
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.