Concurrent Programming Using Java

The material shown here is covered in much more detail in the (forthcoming) book, Concurrent Programming: The Java Programming Language, (to be) published by Oxford University Press in February 1998.

Users of the book can access the book's source code keyed by ``Program/Class'' number and ``Library Class'' number here.

Table of Contents


Sequential Example Programs

    A Simple Genetic Algorithm
    Xtango Algorithm Animation



    Java Monitors
Message Passing
    Synchronization Package Classes
    Example Programs
    Synchronization Package Classes
    Example Programs
    Conditional Rendezvous
Remote Method Invocation

Parallel Processing

    Synchronization Package Classes
    Example Programs
Applet demos of the Xtango Animator

Laboratory Programming Exercises


This is an introduction to using the Java programming language in concurrent or multithreaded applications. The context is the process synchronization material and related concurrent programming in operating systems courses as opposed to software engineering. Topics covered are race conditions when threads share data, critical sections, mutual exclusion, semaphores, monitors, message passing, the rendezvous, remote procedure calls, distributed or network programming, and parallel processing. Solutions to the classical problems talked about in operating systems courses (the dining philosophers, the bounded buffer producers and consumers, and the database readers and writers) are shown in Java. Also shown is how to animate algorithms using the command set of the Xtango animation interpreter, animator. Some of the animation examples can be viewed as applets.

These example programs were developed and tested using Sun Microsystem's JDK version 1.0.2 and 1.1 for Solaris 2.x and Windows 95/NT (1996--97). They have been updated to remove all ``deprecated'' methods and constructors. The multimachine socket examples use the readObject() and writeObject() methods of the ObjectInputStream and ObjectOutPutStream classes, which are part of the RMI (remote method invocation) add-on for JDK 1.0.2 and included with JDK 1.1.

All of the code examples described and hyperlinked here may be retrieved by anonymous ftp from site ( in directory pub/shartley and file bookJavaExamples.tar.gz. This is a GNU gzip-compressed UNIX tar archive file. A zip archive version is also available there. You may also download them directly without using anonymous ftp: gzip tar archive and zip archive.

Java is designed to be a platform-independent language, so all of these examples, including the animated ones, will run without change on Sun's Solaris 2.x UNIX for Sparc and Microsoft Windows 95/NT for Intel-based PCs.

In these compressed archives, there is a directory lib that contains three subdirectories: Utilities, Synchronization, and XtangoAnimation. The path to the lib directory needs to be put into your CLASSPATH environment variable so that your Java programs can import the classes in the subdirectories of lib. For example, suppose you unpack the archive so that lib is in directory /home/you/Java. Then on a UNIX system, put the line

    setenv CLASSPATH /usr/local/JDK/lib/
into your .cshrc or .login file. (Replace /usr/local/JDK/lib/ with wherever your system administrator has installed the Java system classes.)

On a Windows 95/NT system, put the line

in your AUTOEXEC.BAT file (assuming you put lib into D: and the JDK into C:\JAVA).

To test your CLASSPATH setting, try these commands.

    java Utilities.GetOpt
    java XtangoAnimation.XtangoAnimator
These execute the main() methods in those classes, which have been included for testing the classes. You should not get a ``class not found'' error message.

© 1997 Stephen J. Hartley