WoTUG 21
Architectures, Languages and Patterns for Parallel
and Distributed Applications

Java Threads Tutorial

Part 1: Java Threads in the light of occam/CSP

Professor Peter Welch
University of Kent

Abstract. Java provides support for parallel computing through a model that is built into the language itself. However, the designers of Java chose to be fairly conservative and settled for the concepts of threads and monitors. Monitors were developed by Tony Hoare in the early 1970s as a structured way of using semaphores to control access to shared resources. Hoare moved away from this, in the late 1970s, to develop the theory of Communicating Sequential Processes (CSP). One reason for this was that the semantics of monitors and threads are not WYSIWIG, so that designing robust parallel algorithms at this level is seriously hard.

Fortunately, it is possible to introduce the CSP model into Java through sets of classes implemented on top of its monitor support. By restricting interaction between active Java objects to CSP synchronisation primitives, Java thread semantics become compositional and systems with arbitrary levels of complexity become possible. Multi-threaded Web applets and distributed applications become simpler to design and implement, race hazards never occur, difficulties such as starvation, deadlock and livelock are easier to confront and overcome, and performance is no worse than that obtained from directly using the raw monitor primitives.

The advantages of teaching parallelism in Java purely through the CSP class libraries will be discussed. (These libraries were developed jointly at Kent and Oxford Universities in the U.K. and the University of Twente in the Netherlands.)

Part 2: Communicating Java Threads
(The Reference Manual)

Gerald H. Hilderink
University of Twente

Abstract. This manual describes the use of channels and composition constructs for communicating threads in the programming language Java. We assume the reader of this manual is an experienced Java programmer. The code of which the interface is described in this manual is entirely written in Java. We see Java as a clear programming language so that the channel and composition constructs, as described in this reference manual, can be ported to other object-oriented languages, such as C++ or Smalltalk.

Channels are very close to the scheduler, in fact they may be part of the scheduler. Using channels allows scheduling on communication within the program and without any explicit command from the programmer. Scheduling based on communication results in a very fast non-pre-emptive scheduling algorithm that is faster than pre-emptive scheduling to ensure fair scheduling. The program will get better real-time characteristics and the scheduler of the Java Virtual Machine can be tuned for better performance for embedded systems.

Composition constructs provide a more easy way to build robust and better-structured concurrent programs. In chapter 2 the channel interface is described and in chapter 4 the use of the channel is described by three composition (communication) constructs: (1) the sequential communication construct, (2) the parallel communication construct, and (3) the alternative communication constructs. Chapter 5 describes several examples in terms of building blocks that illustrate the use of channels and composition constructs and by which simple concurrent programs can be built.

Page last modified on 12th February 1998