This is a book about using threads in the Java programming language and the Java virtual machine. The topic of threads is very important in Java—so important that many features of a threaded system are built into the Java language itself, while other features of a threaded system are required by the Java virtual machine. Threading is an integral part of using Java.
The concept of threads is not a new one: for some time, many operating systems have had libraries that provide the C programmer with a mechanism to create threads. Other languages, such as Ada, have support for threads embedded into the language, much as support for threads is built into the Java language. Nonetheless, the topic of threads is usually considered a peripheral programming topic, one that’s only needed in special programming cases.
With Java, things are different: it is impossible to write any but the simplest Java program without introducing the topic of threads. And the popularity of Java ensures that many developers who might never have considered learning about threading possibilities in a language like C or C++ need to become fluent in threaded programming.
We’ll start by defining some terms used throughout this book. Many terms surrounding Java are used inconsistently in various sources; we’ll endeavor to be consistent in our usage of these terms throughout the book.
- Java
First is the term Java itself. As we know, Java started out as a programming language, and many people today think of Java as being simply a programming language. But Java is much more than just a programming language: it’s also an API specification and a virtual machine specification. So when we say Java, we mean the entire Java platform: a programming language, an API, and a virtual machine specification that, taken together, define an entire programming and runtime environment. Often when we say Java, it’s clear from context that we’re talking specifically about the programming language, or parts of the Java API, or the virtual machine. The point to remember is that the threading features we discuss in this book derive their properties from all the components of the Java platform taken as a whole. While it’s possible to take the Java programming language, directly compile it into assembly code, and run it outside of the virtual machine, such an executable may not necessarily behave the same as the programs we describe in this book.
- Virtual machine, interpreters, and browsers
The Java virtual machine is another term for the Java interpreter, which is the code that ultimately runs Java programs by interpreting the intermediate byte-code format of the Java programming language. The Java interpreter actually comes in three popular forms: the interpreter for developers (called java) that runs programs via the command line or a file manager, the interpreter for end users (called jre ) that is a subset of the developer environment and forms the basis of (among other things) the Java plug-in, and the interpreter that is built into many popular web browsers such as Netscape Navigator, Internet Explorer, HotJava™, and the appletviewer that comes with the Java Developer’s Kit. All of these forms are simply implementations of the Java virtual machine, and we’ll refer to the Java virtual machine when our discussion applies to any of them. When we use the term Java interpreter, we’re talking specifically about the command-line, standalone version of the virtual machine (including those virtual machines that perform just-in-time compilation); when we use the term Java-enabled browser (or, more simply, browser), we’re talking specifically about the virtual machine built into web browsers.
For the most part, virtual machines are indistinguishable—at least in theory. In practice, there are a few important differences between implementations of virtual machines, and one of those differences comes in the world of threads. This difference is important in relatively few circumstances, and we’ll discuss it in Chapter 6.
- Programs, applications, and applets
This leads us to the terms that we’ll use for things written in the Java language. Generically, we’ll call such entities programs. But there are two types of programs a typical Java programmer might write: programs that can be run directly by the Java interpreter and programs designed to be run by a Java-enabled browser.[1] Much of the time, the distinction between these two types of Java programs is not important, and in those cases, we’ll refer to them as programs. But in those cases where the distinction is important, we’ll use the term applets for programs running in the Java-enabled browser and the term applications for standalone Java programs. In terms of threads, the distinction between an applet and an application manifests itself only in Java’s security model; we’ll discuss the interaction between the security model and Java threads in Chapter 10.
[1] Though it’s possible to write a single Java program so that it can be run both by the interpreter and by a browser, the distinction still applies at the time the program is actually run.
Get Java Threads, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.