Using Threads as Actors


Prerequisites

This Magercise takes the WaitStack and uses it as an input stack for an actor-style class. Each actor class is called a CatMachine and takes some number of its inputs, concatentates them together, and feeds it to an assigned output CatMachine.

The starter file includes the WaitStack class and the applet CatTest.

Some aspects of using threads in program design are covered in the introduction to the course notes.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Using Threads as Actors.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Create the new interface Consumer, it has one method, inputItem, which has one argument, an Object, and returns no value.

  2. Create a class that implements the Consumer interface, CatMachine. Use a WaitStack as it's input storage. The arguments to the constructor of this class should take two arguments, an integer to specify the number of stack items to look for, and a Consumer object to use as an output. The run method for this class will pop the specified number of items off the stack, concatenate them together as strings, then send them to the output consumer.

  3. Create a subclass of TextArea that implements the Consumer interface by adding its input to its content.

  4. Create a group of three CatMachines that feed the TextArea. They should be arranged so that the first two CatMachines feed the third, which in turn feeds the TextArea.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.