Home > Store

Concurrent Programming Javaâ„¢: Design Principles and Patterns

Register your product to gain access to bonus material or receive a coupon.

Concurrent Programming Javaâ„¢: Design Principles and Patterns

Book

  • Sorry, this book is no longer in print.
Not for Sale

About

Overview

Concurrency introduces design and programming opportunities and problems not found in sequential OO programming. This book describes some ways to exploit the opportunities and solve the problems, mainly by reusing constructions and techniques that others have found to be useful solutions to common concurrent OO design problems. Thread packages that are not integrated into programming languages tend to be pretty hard to use. Fat manuals are often needed just to figure out how to express simple constructions. This is not so in Java. The latter part of this chapter surveys the few Java constructs specifically related to concurrent programming. (It assumes that you are familiar with other aspects of Java programming.) The rest of the book focuses on the roles and uses of these constructs in the design of classes, components, frameworks, and applications.

This book is about design, not about specialized concurrent algorithms or their formal analysis. There are already many good texts (in particular the one by Andrews listed in the Further Readings) presenting algorithms that can be implemented in Java. This book instead collects standard design techniques from non-OO concurrent programming, useful constructions found in other concurrent programming languages, new ideas from the research literature on OO concurrency, and practical considerations from the application of concurrency in actual software development. It presents them in a way that is meant to be used and reused in constructing concurrent Java components, applets, and applications.

Some concepts are introduced by first showing what they would look like using pidgin-Java equivalents of constructs found in the languages and systems that introduced the ideas, and then showing how to obtain the effects in Java proper. This provides different conceptual tools for designing concurrent components. It also makes it easier for you to adapt techniques not covered here that originate from other languages or other approaches to concurrency.

Design patterns are used to help organize the wealth of techniques available for structuring concurrent Java programs. A pattern describes a design form, usually an object structure (also known as a micro-architecture) consisting of one or more interfaces, classes, and/or objects that obey certain static and dynamic constraints and relationships. Patterns are an ideal vehicle for presenting designs and techniques that need not be implemented in exactly the same way across different contexts, and thus cannot be usefully encapsulated as reusable components. Reusable components and frameworks can play a central role in software development. But much of concurrent OO programming consists of the reuse, adaptation, and extension of recurring design forms and practices rather than of particular classes.

A pattern-based approach also helps bridge the unfortunate gap between theory and practice in concurrent programming. Research on concurrency sometimes relies on models and techniques that are ill-suited for everyday OO software development. At the other extreme, some thread-based programs are the result of reckless hacking. This book tries to take a middle ground, stealing good ideas and best practices from everywhere and codifying them into readily applicable forms.

Most of the terminology and notation used here is adapted from the pioneering book Design Patterns by Gamma, Helm, Johnson, and Vlissides (see the Further Readings). Some of the patterns presented here are extensions or applications of common sequential OO design patterns to concurrent programming problems. In particular, most of the patterns presented in Design Patterns are used or referenced in this book. These are summarized upon first encounter, although only very briefly in cases of tangential tie-ins. You can ignore these references if you have not yet read Design Patterns.

Unlike those in Design Patterns, the patterns here are embedded within chapters discussing sets of related contexts and software design principles that generate the main forces and constraints resolved in the patterns. This presentation style helps organize an otherwise bewildering array of patterns that range from tiny idiomatic programming constructions to application-level structuring techniques. Still, this book maintains a pattern-based approach, clarifying underlying design forms, contexts, applicability, and consequences. It describes solutions in a constructive, recipe-like fashion. In fact, many patterns include design steps in addition to presentations of object structures, and so are a bit more recipe-like than those in Design Patterns.

Because this book concentrates on Java in particular, most pattern descriptions are less complete and wide-ranging than those in Design Patterns. For example, there are few discussions of known uses that reference larger examples in existing systems. Even though these patterns are seen in existing Java programs, the language is still too new for there to be many significant usage examples. Also, some patterns focus on object-oriented designs that, once established, allow you to exploit concurrency techniques that are covered in readily available sources in more extensive detail than can be accommodated here.

Most techniques and patterns in this book are illustrated by variants of an annoyingly small set of toy running examples. This is not an effort to be boring, but to be clear. Concurrency constructs are often subtle enough to get lost in other wise meaningful examples. Reuse of small examples makes small but critical differences across patterns more obvious. Also, the presentations include many code sketches and snippets illustrating Java implementation techniques.

Most concurrent Java applications use only a few of the designs presented in this book, as appropriate to the problems at hand. All of these designs can be implemented in Java 1.0. Many of the examples rely on classes in the standard Java packages, including java.awt and java.applet. But no special concurrency properties of these packages are relied on. No other special tools or extensions are needed.

Chapter Preview

Each chapter alternates between general discussions of concepts and principles and pattern-style presentations of particular design forms. Each chapter concludes with a Further Readings section listing related books and articles, sometimes accompanied by minor points omitted from the text.

The remainder of the chapter presents introductory examples of Java concurrency constructs and a reference summary of their properties. The Further Readings section contains a master list of general-purpose sources on issues related to concurrency and OO development.

Chapter 2 discusses the central notion of safety in multithreaded contexts and presents three conservative strategies (based on immutability, synchronization, and containment) that result in safe designs.

Chapter 3 discusses the equally central notion of liveness and presents two general-purpose techniques (analyzing instance variables and splitting synchronization) that can avoid or reduce liveness and efficiency problems.

Chapter 4 deals with designs applicable when actions are state-dependent, that is, cannot be guaranteed to be successful unless objects are in appropriate states. The chapter describes state-based techniques for preventing and delaying actions, along with those for recovering from undesired effects.

Chapter 5 presents three approaches to concurrency control, the layering of synchronization and control over basic functionality. Subclassing, delegation, and meta-level control provide the basis for composing concurrent components in a bottom-up fashion.

Chapter 6 lays out options for creating and invoking threads to perform services. It presents a set of design choices, implementation techniques, and patterns that can be tailored to particular contexts and applications.

Chapter 7 describes flow architectures -- application-level patterns for structuring multithreaded activities by standardizing communication patterns among objects serving as producers and consumers.

Chapter 8 surveys three approaches to coordinating the interaction of cooperating, independent objects across multiple threads: Transaction, notification, and scheduling techniques provide tools for attacking the intrinsic complexity of cooperative designs.

The on-line supplement can be accessed on the World Wide Web via links from http://java.sun.com/Series. The supplement includes:

  • All source code, including full versions of examples that are only briefly sketched in this book and those implemented as applets that can be run to get a better feel for the dynamics of concurrent programs.

  • Example applications. To compensate for lack of variety in examples, the supplement includes several applications that demonstrate issues faced when using patterns to build programs.

  • Links to sites containing related information. When possible, references in this book list easily accessible books and articles. The supplement contains links to papers, reports, and web pages available electronically.

  • Hyperlinked synopses of patterns described in the book, showing their dependencies and relations. The concepts and discussions in this book are organized sequentially. But the patterns presented in each chapter can be used with others from distant chapters. The hyperlinked synopses make it easier to put patterns into practice once you know why they exist.

  • Other extensions, supplements, corrections, contributed examples, and commentary produced after this book went to press.

Description

  • Copyright 1997
  • Dimensions: 7-3/8x9-1/4
  • Pages: 352
  • Edition: 1st
  • Book
  • ISBN-10: 0-201-69581-2
  • ISBN-13: 978-0-201-69581-6

Second Edition
now available!

"One of the most important features of Java is its support for concurrent programming. This book provides an introduction to the many ways of structuring concurrent applications, and is a 'must read' for programmers who wish to structure concurrent Java applications."
-Bill Joy, Co-Founder and Vice President of Research, Sun Microsystems

One of Java's most powerful capabilities is its built-in support for threads, making concurrent programming a viable option for Java development. As a serious Java programmer interested in using this rich language to the fullest, a mastery of thread programming will significantly enhance your ability to create high-performance Java applications.

This book provides you with detailed information and expert techniques that will enable you to exploit the many advantages of concurrent programming and create multi-threaded Java applications that are more responsive to user demands, faster, and more easily controlled. Taking a design pattern approach, the book offers numerous standard design techniques for creating and implementing Java structures that solve common concurrent programming challenges.

You will learn how to initiate, control, and coordinate concurrent activities using the Java constructs java.lang.Thread, the keywords synchronized and volatile, and the methods wait, notify, and notifyAll. In addition, you will find detailed coverage of all aspects of thread programming, including such topics as containment and synchronization, deadlocks and conflicts, state-dependent action, control, how to handle user services, flow, and coordinated interaction. Code examples throughout help clarify the subtleties of the concurrent programming concepts discussed.



0201695812B04062001

Downloads

Source Code

Click below for Source Code related to this title:
Source Code

Sample Content

Preface

The Java Series books provide definitive reference documentation for Java programmers and end users. They are written by members of the Java team and published under the auspices of JavaSoft, a Sun Microsystems business. The World Wide Web allows Java documentation to be made available over the Internet, either by downloading or as hypertext. Nevertheless, the worldwide interest in Java technology led us to write and publish these books to supplement all of the documentation at our Web site.

To learn the latest about the Java Platform and Environment, or to download the latest Java release, visit our World Wide Web site at http://java.sun.com. For updated information about The Java Series, including sample code, errata, and previews of forthcoming books, visit http://java.sun.com/Series.

We would like to thank the Corporate and Professional Publishing Group at Addison-Wesley for their partnership in putting together the Series. Our editor Mike Hendrickson and his team have done a superb job of navigating us through the world of publishing. Within Sun, the support of James Gosling, Ruth Hennigar, Jon Kannegaard, and Bill Joy ensured that this series would have the resources it needed to be successful. In addition to the tremendous effort by individual authors, many members of the JavaSoft team have contributed behind the scenes to bring the highest level of quality and engineering to the books in the Series. A personal note of thanks to my children Christopher and James for putting a positive spin on the many trips to my office during the development of the Series.

Lisa Friendly, Series Editor

Foreword

The Java programming language has taken the world by storm, fueled by growing public exposure to object-oriented programming languages such as C++ and Smalltalk, and launched by the explosion of the Internet. Among other things, Java offers simple and tightly-integrated support for threads, and as a result has given many people their first exposure to concurrent programming.

A concurrent program consists of multiple tasks that behave as though they are all in progress at one time. On a computer system with multiple processors, this may literally be true -- several tasks may execute at once, each on its own processor. On a system with a single processor, or where there are more tasks than processors, the system can switch between tasks, making it appear that more tasks than there are processors are executing at once.

The tasks of a concurrent program are often implemented using threads (short for "threads of control"), sequences of instructions that run independently within the encompassing program. Threads may share access to memory with other threads, and may have means of coordinating their activities with those of other threads, the computer system, or the user to achieve the goals of the program.

Writing concurrent programs requires looking at things in ways that are outside the experience of many programmers. Gaining this new perspective can be difficult. Many threads packages are complex, and perhaps as a result their documentation bogs down in a nuts-and-bolts style. Some approaches are academic and relatively inaccessible. As a result, programming with threads has remained more or less a black art, practiced by the few who manage to figure it all out somehow. Of those, not all learn to use threads elegantly or reliably.

Despite the relative simplicity of Java's threading model, writing concurrent programs in Java can still be strange and confusing. Most treatments of the Java language cover concurrent programming in a single chapter that introduces Java's thread classes but doesn't give the reader the conceptual background to program with threads effectively.

This is the first book dedicated to concurrent programming in Java. Doug Lea extends the object-oriented design paradigm using the now-familiar patterns approach of books like Design Patterns (Gamma, Helm, Johnson, Vlissides). He bridges the gap between typical programming experience and reference material by providing the conceptual structure necessary to design efficient, reliable, and elegant concurrent programs.

While covering programming with Java threads in detail, Doug shows the reader how to think about concurrent programming in a way that is equally appropriate when using other approaches to concurrent programming -- not just when using Java. In the process he presents a pragmatic cookbook of design patterns for concurrent programming that will be instructive and useful for new threads programmers and experienced programmers alike.

Tim Lindholm, JavaSoft
September, 1996

Acknowledgments

This book began as a small set of Web pages that I put together in spring 1995, while trying to make sense of my own early attempts to use Java concurrency features in experimental development efforts. Then it grew; first on the World Wide Web, where I extended, expanded, and removed patterns to reflect my and other people's increasing experience with Java concurrency; and now into this book, which places patterns within the perspective of concurrent software development principles. The web pages also live on, but they now serve as a supplement to the conceptual presentations best suited to book form.

There have been many changes along the way, in a process that has benefited from commentary, suggestions, and exchanges with many kind and knowledgeable people. They include Taranov Alexander, Il-Hyung Cho, Bruce Eckel, Ed Falis, Randy Farmer, Alain Hsiung, Johannes Johannsen, Istvan Kiss, Jonathan Locke, Mike Mills, Trevor Morris, Andrew Purshottam, Simon Roberts, Joel Rosi-Schwartz, Aamod Sane, Doug Schmidt, Kevin Shank, Sumana Srinivasan, Henry Story, Satish Subramanian, Jeff Swartz, Patrick Thompson, Volker Turau, Cees Vissar, Bruce Wallace, Greg Wilson, and Steve Yen, as well as the many people who submitted anonymous electronic mail commentary.

The members of Ralph Johnson's patterns seminar (especially Brian Foote and Ian Chai) read through early forms of some patterns and suggested many improvements. Official and unofficial reviewers of the book manuscript also made substantial contributions. They include Ken Arnold, Joseph Bowbeer, Patrick Chan, Gary Craig, Desmond D'Souza, Tim Harrison, David Henderson, David Holmes, Tim Lindholm, James Robins, Greg Travis, Mark Wales, and Deborra Zukowski. Very special thanks go to Tom Cargill for his many insights and corrections over the past year, as well as for permission to include a description of his Specific Notification pattern (in Chapter 8).

Rosemary Simpson contributed numerous improvements in the course of creating the index. Ken Arnold patiently helped me deal with FrameMaker. Mike Hendrickson and the editorial crew at Addison-Wesley have been continually supportive.

This book would not have been possible without the generous support of Sun Labs. Thanks especially to Jos Marlowe for providing opportunities to work collaboratively on fun and exciting research and development projects.

Thanks above all to Kathy, Keith, and Colin for tolerating all this.

Doug Lea
September, 1996



0201695812P04062001

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.