Active Objects

An object is the software equivalent of a computer chip. It has memory (fields) and logic (methods). In addition, an active object is also equipped with its own virtual machine (VM).

An active object's VM can execute a perpetual control loop:

1. inspect environment
2. if (goals reached) quit
3. select a method (of mine or some other object's)
4. execute method (using my VM)
5. goto 1

Of course several active objects can be executing their control loops at the same time. To achieve this magic the operating system regards each VM as a sequence or thread of instructions that need to be executed. The operating system executes a few instructions from VM1, then a few from VM2, and so forth until it's VM1's turn again. Switching from one thread to another happens so quickly that the illusion of parallel processing is created.

Example

A user interface object perpetually listens for user inputs. When a user input arrives, the user interface launches a background task to process the user's inputs, then resumes listening for more inputs (such as the command to cancel the background task.)

Example

A server object perpetually listens for client requests. When a request arrives, the server launches a request handler object that services the client, then resumes listening. Request handlers are also active objects.

Example

An object that performs some background task such as garbage collection periodically monitors the environment to see if the task needs to be performed, then performs it if necessary.

Example

A master object launches active slave objects to solve related sub-problems. When all of the slaves are finished, the master compiles their solutions into the solution to the main problem.

UML Notation

Active objects are instances of active classes. The UML icon for an active class uses double lines of boldface for its perimeter:

The Lifecycle of an Active Object

An active object can be in one of several basic states:

ready: waiting for the CPU
running: using the CPU
blocked: waiting for an event such as a timeout or user input
terminated: the task is completed

We can show the transitions between these states using a UML statechart diagram:

Notice that it is the operating system's job to allocate the CPU to the active objects that are ready for it. This is done by organizing the active objects into a ready queue.

Active objects can also have priorities such as HIGH, MEDIUM, and LOW. Active objects that are ready for the CPU are placed in the ready queue according to their priority.

If a running CPU uses up too much CPU time without exiting or suspending, some operating system's will interrupt the active object and put it back in the ready queue. This is called preemption.

Synchronization

Synchronization problems can arise when several active objects simultaneously inspect and update a shared variable, object, or file. For example, what happens when a husband and wife, using different ATM machines attempt to withdraw the last $50 from their joint account at the same moment? Do they both get $50? Do neither get $50?

To prevent synchronization problems operating systems provide software locks that can be tested and set in a single indivisible step. Providing the joint account with such a lock means that whoever locks the account first gets the $50.

Multithreading in Java

Multithreading in C++