Multithreaded Beverages Application

   

The following scenario creates an console application that supports two threads. The application consists of three classes: Class1, Coffee, and Tea. Class1 contains the application’s main()method and code to start two threads — one to run the code for the Coffee class and one for the Tea class.

To create a multithreaded console application

  1. On the File menu, click New Project to display the New Project dialog box.

  2. Expand the folders for Visual J++ Projects and then Applications.

  3. Single-click the Console Application icon.

  4. In the Name text box, type a unique name for your project.

  5. In the Location text box, enter or Browse to the location where you want to save your project.

  6. Click Open and your project appears in Project Explorer.

  7. In Project Explorer, expand your project’s icon and double-click on the Class1.java file.

    Your .java file is now loaded in the Text editor and ready for modifications.

To add the Coffee and Tea classes to Class1.java file

  1. At the end of the file, after Class1’s closing brace, “{“, enter the code found in Coffee and Tea Source Code.

  2. Between the braces of Class1’s main() method, insert the following code:
    Coffee m_Coffee = new Coffee();  //creates Coffee object
    m_Coffee.start();                      //creates thread for Coffee object
    new Tea().start();                  //creates Tea object and its thread