Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_concurrency_in_practice.pdf
Скачиваний:
104
Добавлен:
02.02.2015
Размер:
6.66 Mб
Скачать

xiv Java Concurrency In Practice

Chapters 2 (Thread Safety) and 3 (Sharing Objects) form the foundation for the book. Nearly all of the rules on avoiding concurrency hazards, constructing thread safe classes, and verifying thread safety are here. Readers who prefer

"practice" to "theory" may be tempted to skip ahead to Part II, but make sure to come back and read Chapters 2 and 3 before writing any concurrent code!

Chapter 4 (Composing Objects) covers techniques for composing thread safe classes into larger thread safe classes.

Chapter 5 (Building Blocks) covers the concurrent building blocks thread safe collections and synchronizers provided by the platform libraries.

Structuring Concurrent Applications. Part II (Chapters 6 9) describes how to exploit threads to improve the throughput or responsiveness of concurrent applications. Chapter 6 (Task Execution) covers identifying parallelizable tasks and executing them within the task execution framework. Chapter 7 (Cancellation and Shutdown) deals with techniques for convincing tasks and threads to terminate before they would normally do so; how programs deal with cancellation and shutdown is often one of the factors that separate truly robust concurrent applications from those that merely work.

Chapter 8 (Applying Thread Pools) addresses some of the more advanced features of the task execution framework.

Chapter 9 (GUI Applications) focuses on techniques for improving responsiveness in single threaded subsystems.

Liveness, Performance, and Testing. Part III (Chapters 10 12) concerns itself with ensuring that concurrent programs actually do what you want them to do and do so with acceptable performance. Chapter 10 (Avoiding Liveness Hazards) describes how to avoid liveness failures that can prevent programs from making forward progress. Chapter 11

(Performance and Scalability) covers techniques for improving the performance and scalability of concurrent code.

Chapter 12 (Testing Concurrent Programs) covers techniques for testing concurrent code for both correctness and performance.

Advanced Topics. Part IV (Chapters 13 16) covers topics that are likely to be of interest only to experienced developers:

explicit locks, atomic variables, non blocking algorithms, and developing custom synchronizers.

Code Examples

While many of the general concepts in this book are applicable to versions of Java prior to Java 5.0 and even to non Java environments, most of the code examples (and all the statements about the Java Memory Model) assume Java 5.0 or later. Some of the code examples may use library features added in Java 6.

The code examples have been compressed to reduce their size and to highlight the relevant portions. The full versions of the code examples, as well as supplementary examples and errata, are available from the book's website, http://www.javaconcurrencyinpractice.com.

The code examples are of three sorts: "good" examples, "not so good" examples, and "bad" examples. Good examples

illustrate techniques that should be emulated. Bad examples illustrate techniques that should definitely not be emulated, and are identified with a "Mr. Yuk" icon[1] to make it clear that this is "toxic" code (see Listing 1). Not so good

examples illustrate techniques that are not necessarily wrong but are fragile, risky, or perform poorly, and are decorated with a "Mr. CouldBeHappier" icon as in Listing 2.

[1] Mr. Yuk is a registered trademark of the Children's Hospital of Pittsburgh and appears by permission.

Listing 1. Bad Way to Sort a List. Don't Do this.

public <T extends Comparable<? super T>> void sort(List<T> list) { // Never returns the wrong answer!

System.exit(0);

}

Some readers may question the role of the "bad" examples in this book; after all, a book should show how to do things right, not wrong. The bad examples have two purposes. They illustrate common pitfalls, but more importantly they demonstrate how to analyze a program for thread safety and the best way to do that is to see the ways in which thread safety is compromised.

2BPreface xv

Listing 2. Less than Optimal Way to Sort a List.

public <T extends Comparable<? super T>> void sort(List<T> list) { for (int i=0; i<1000000; i++)

doNothing();

Collections.sort(list);

}

Acknowledgments

This book grew out of the development process for the java.util.concurrent package that was created by the Java

Community Process JSR 166 for inclusion in Java 5.0. Many others contributed to JSR 166; in particular we thank Martin Buchholz for doing all the work related to getting the code into the JDK, and all the readers of the concurrencyinterest mailing list who offered their suggestions and feedback on the draft APIs.

This book has been tremendously improved by the suggestions and assistance of a small army of reviewers, advisors,

cheerleaders, and armchair critics. We would like to thank Dion Almaer, Tracy Bialik, Cindy Bloch, Martin Buchholz, Paul

Christmann, Cliff Click, Stuart Halloway, David Hovemeyer, Jason Hunter, Michael Hunter, Jeremy Hylton, Heinz Kabutz,

Robert Kuhar, Ramnivas Laddad, Jared Levy, Nicole Lewis, Victor Luchangco, Jeremy Manson, Paul Martin, Berna

Massingill, Michael Maurer, Ted Neward, Kirk Pepperdine, Bill Pugh, Sam Pullara, Russ Rufer, Bill Scherer, Jeffrey Siegal,

Bruce Tate, Gil Tene, Paul Tyma, and members of the Silicon Valley Patterns Group who, through many interesting technical conversations, offered guidance and made suggestions that helped make this book better.

We are especially grateful to Cliff Biffle, Barry Hayes, Dawid Kurzyniec, Angelika Langer, Doron Rajwan, and Bill Venners, who reviewed the entire manuscript in excruciating detail, found bugs in the code examples, and suggested numerous improvements.

We thank Katrina Avery for a great copy editing job and Rosemary Simpson for producing the index under unreasonable time pressure. We thank Ami Dewar for doing the illustrations.

Thanks to the whole team at Addison Wesley who helped make this book a reality. Ann Sellers got the project launched

and Greg Doench shepherded it to a smooth completion; Elizabeth Ryan guided it through the production process.

We would also like to thank the thousands of software engineers who contributed indirectly by creating the software used to create this book, including TEX, LATEX, Adobe Acrobat, pic, grap, Adobe Illustrator, Perl, Apache Ant, IntelliJ

IDEA, GNU emacs, Subversion, TortoiseSVN, and of course, the Java platform and class libraries.

3BChapter 1 Introduction 10B1.1. A (Very) Brief History of Concurrency 1

Chapter 1ǦIntroduction

Writing correct programs is hard; writing correct concurrent programs is harder. There are simply more things that can go wrong in a concurrent program than in a sequential one. So, why do we bother with concurrency? Threads are an inescapable feature of the Java language, and they can simplify the development of complex systems by turning complicated asynchronous code into simpler straight line code. In addition, threads are the easiest way to tap the computing power of multiprocessor systems. And, as processor counts increase, exploiting concurrency effectively will only become more important.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]