The Art of Multiprocessor Programming

This appendix describes the basic programming language constructs needed to understand our examples and to write your own concurrent programs. Mostly, we use Java, but the same ideas could be equally well expressed in other high-level languages and libraries. Here, we review the basic software concepts needed to understand this text, first in Java, and then in other important models such as C# or the Pthreads library for C and C++. Unfortunately, our discussion here must be incomplete: if in doubt, consult the current documentation for the language or library of interest.
The Java programming language uses a concurrency model in which threads and objects are separate entities. [1] Threads manipulate objects by calling the objects' methods, coordinating these possibly concurrent calls using various language and library constructs. We begin by explaining the basic Java constructs used in this text.
A thread executes a single, sequential program. In Java, a thread is usually a subclass of java.lang.Thread, which provides methods for creating threads, starting them, suspending them, and waiting for them to finish.
First, create a class that implements the Runnable interface. The class's run() method does all the work. For example, here is a simple thread that prints a string.
<b class="bold">public class</b> HelloWorld <b class="bold">implements</b> Runnable { String message; <b class="bold">public</b> HelloWorld(String m) { ...