Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596007829/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
When Sun Microsystems released the alpha version of Java in the winter of 1995, developers all over the world took notice. There were many features of Java that attracted these developers, not the least of which were the set of buzzwords Sun used to promote the language. Java was, among other things, robust, safe, architecture-neutral, portable, object-oriented, simple, and multithreaded. For many developers, these last two buzzwords seemed contradictory: how could a language that is multithreaded be simple?
It turns out that Javas threading system is simple, at least relative to other threading systems. This simplicity makes Javas threading system easy to learn so that even developers who are unfamiliar with threads can pick up the basics of thread programming with relative ease.
In early versions of Java, this simplicity came with tradeoffs; some of the advanced features that are found in other threading systems were not available in Java. Java 2 Standard Edition Version 5.0 (J2SE 5.0) changes all of that; it provides a large number of new thread-related classes that make the task of writing multithreaded programs that much easier.
Still, programming with threads remains a complex task. This book shows you how to use the threading tools in Java to perform the basic tasks of threaded programming and how to extend them to perform more advanced tasks for more complex programs.
Who Should Read This Book?
This book is intended for programmers of all levels who need to learn to use threads within Java programs. This includes developers who have previously used Java and written threaded programs; J2SE 5.0 includes a wealth of new thread-related classes and features. Therefore, even if youve written a threaded program in Java, this book can help you to exploit new features of Java to write even more effective programs.
The first few chapters of the book deal with the issues of threaded programming in Java, starting at a basic level; no assumption is made that the developer has had any experience in threaded programming. As the chapters progress, the material becomes more advanced, in terms of both the information presented and the experience of the developer that the material assumes. For developers who are new to threaded programming, this sequence should provide a natural progression of the topic.
This book is ideally suited to developers targeting the second wave of Java programsmore complex programs that fully exploit the power of Javas threading system. We make the assumption that readers of the book are familiar with Javas syntax and features. In a few areas, we present complex programs that depend on knowledge of other Java features: AWT, Swing, NIO, and so on. However, the basic principles we present should be understandable by anyone with a basic knowledge of Java. Weve found that books that deal with these other APIs tend to give short shrift to how multiple threads can fully utilize these features of Java (though doubtless the reverse is true; we make no attempt to explain nonthread-related Java APIs).
Though the material presented in this book does not assume any prior knowledge of threads, it does assume that the reader has knowledge of other areas of the Java API and can write simple Java programs.
Versions Used in This Book
Writing a book on Java in the age of Internet time is hardthe sand on which were standing is constantly shifting. But weve drawn a line in that sand, and the line weve drawn is at the Java 2 Standard Edition (J2SE) Version 5.0 from Sun Microsystems. This software was previously known as J2SE Version 1.5.
Its likely that versions of Java that postdate this version will contain some changes to the threading system not discussed in this edition of the book. We will also point out the differences between J2SE 5.0 and previous versions of Java as we go so that developers using earlier releases of Java will also be able to use this book.
Most of the new threading features in J2SE 5.0 are available (with different APIs) from third-parties for earlier versions of Java (including classes we developed in earlier editions of this book). Therefore, even if youre not using J2SE 5.0, youll get full benefit from the topics covered in this book.
Whats New in This Edition?
This edition includes information about J2SE 5.0. One of the most significant changes in J2SE 5.0 is the inclusion of Java Specification Request (JSR) 166, often referred to as the "concurrency utilities. JSR-166 specifies a number of thread-related enhancements to existing APIs as well as providing a large package of new APIs.
These new APIs include:
Atomic variables
A set of classes that provide threadsafe operations without synchronization
Explicit locks
Synchronization locks that can be acquired and released programmatically
Condition variables
Variables that can be the subject of a targeted notification when certain conditions exist
Queues
Collection classes that are thread-aware
Synchronization primitives
New classes that perform complex types of synchronization
Thread pools
Classes that can manage a pool of threads to run certain tasks
Thread schedulers
Classes that can execute tasks at a particular point in time
Weve fully integrated the new features of J2SE 5.0 throughout the text of this edition. The new features can be split into three categories:
New implementations of existing features
The Java language has always had the capability to perform data synchronization and thread notification. However, implementation of these features was somewhat limited; you could, for example, synchronize blocks of code or entire methods but synchronizing across methods and classes required extra programming. In J2SE 5.0, explicit locks and condition variables allow you more flexibility when using these features.
These new implementations do not introduce new concepts for a developer. A developer who wants to write a threadsafe program must ensure that her data is correctly synchronized, whether she uses J2SE 5.0s explicit locks or the more basic