taming java threads pdf_Allen Holub - Taming Java Threads - Apress.pdf

44cb7578e1df5412b94317daaa3307ba.gifAllen Holub - Taming Java Threads - Apress.pdf

2 Table of Contents Taming Java Threads Preface Chapter 1 The Architecture of Threads Chapter 2 The Perils of Multithreaded Programming Chapter 3 The Mutex and Lock Management Chapter 4 Condition Variables and Counting Semaphores Chapter 5 Timers Alarms and Swing Thread Safety Chapter 6 Observers and Multicasters Chapter 7 Singletons Critical Sections and Reader Writer Locks Chapter 8 Threads in an Object Oriented World Chapter 9 Object Oriented Threading Architectures Chapter 10 If I Were King Fixing Java s Threading Problems Index List of Figures List of Tables List of Listings List of Sidebars 3 Taming Java Threads 7 Preface 8 Prerequisites 8 Getting and Using the Code 8 Chapter 1 The Architecture of Threads 10 The Problems with Threads 10 All Nontrivial Java Programs Are Multithreaded 10 Java s Thread Support Is Not Platform Independent 12 Threads and Processes 13 Thread Safety and Synchronization 13 Synchronization Is Expensive 14 Avoiding Synchronization 18 Concurrency or How Can You Be Two Places at Once When You re Really Nowhere at All 22 Get Your Priorities Straight 23 Cooperate 24 The Cooperative Multithreading Model 24 The Preemptive Multithreading Model 25 Mapping Kernel Threads to User Processes 25 Wrapping Up 28 Chapter 2 The Perils of Multithreaded Programming 29 Monitors and Exclusion Semaphores Mutex 29 Race Conditions and Spin Locks 30 The Spin lock Class 35 Threads Are Not Objects 39 Deadlock 41 Get out the Magnifying Glass 42 Nested Monitor Lockout 44 Synchronization Wrappers 45 Time Out 46 A Digression on Style 49 Why Is suspend Deprecated 50 Deadlock on a Blocking I O Operation 51 Stopping Threads 54 Starvation and Synchronizing run 55 The volatile Keyword 57 Exceptions and Threads 58 4 Conclusion 60 Chapter 3 The Mutex and Lock Management 61 When synchronized Isn t Good Enough 61 Handling Granularity with synchronized 62 Roll Your Own Semaphores The Semaphore Interface 63 Managing Semaphores and Deadlock Resistant Locking 66 A Digression Booch Utilities and Strategy 70 Implementing a Manageable Mutex Class 71 Conclusion 77 Chapter 4 Condition Variables and Counting Semaphores78 Condition Variables 78 Waiting for the Electrician or Somebody Like Him Condition Variables vs wait 78 Send in the Cavalry Using a Condition Variable 81 Implementing a Condition Variable 89 Condition Sets Waiting for Multiple Conditions 98 Counting Semaphores for Managing Resource Pools 106 Wrapping Up 115 Chapter 5 Timers Alarms and Swing Thread Safety 116 Why a Timer 116 Swingin Threads Swing Isn t Thread Safe 117 The invokeLater and invokeAndWait Methods 118 Using the Swing Timer 119 So How Does It Work 121 Why Use a Swing Timer or Not 121 Roll Your Own Timer Implementing the Alarm Class 122 The Static Structure of an Alarm 124 Dissecting a Notification 125 Restarting an Alarm Stopping a Thread 141 Suspending the Clock 142 Notifier Problems 142 Unit Tests 143 Summing Up 144 Chapter 6 Observers and Multicasters 145 Implementing Observer in a Multithreaded World 145 Observer side Problems Inner class Synchronization 146 Notifier side Problems Notifications in a Multithreaded World 149 Mysteries of the AWTEventMulticaster 157 Immutable Objects and Blank Finals 158 Using the Multicaster 159 Building a Multicaster 160 5 Chapter 7 Singletons Critical Sections and Reader Writer Locks 168 Overview 168 Critical Sections Singletons and the Class Object 168 Static Members 168 Singletons 172 Critical Sections Doubled checked Locking and Cache related Problems in Multiple CPU Machines 174 The Std Class An Example of Singleton 176 Closing Singletons 184 Reader Writer Locks 185 It s a Wrap 198 Chapter 8 Threads in an Object Oriented World 199 Overview 199 Modeling Threads in Object Oriented Systems 199 Synchronous vs Asynchronous Messages 199 Implementing Asynchronous Messages Using Thread per Method 200 An Exceptional Problem 205 Thread Pools and Blocking Queues 206 Blocking Queues 208 Pooling Threads 220 Passing Arguments to the Operation 221 Using Introspection for Runnable Objects that Pass Arguments 222 Implementing the Thread pool 228 Putting the Pool to Work 239 Sockets and Thread Pools 240 Conclusion 253 Chapter 9 Object Oriented Threading Architectures 254 Reactors and Active Objects 254 Synchronous Dispatching and Round Robin Scheduling Reactors and Proactors 255 Asynchronous Dispatching Active Objects 266 A General Solution 267 Detangling Console Output 272 That s It 278 Chapter 10 If I Were King Fixing Java s Threading Problems 280 Overview 280 The Task 280 Improvements to synchronized 283 Improvements to wait and notify 284 6 Fixing the Thread Class 285 Inter Thread Coordination 285 Internal Support for Reader Writer Locks 286 Access to Partially Constructed Objects Should Be Illegal 286 Volatile Should Always Work as Expected 288 Access Issues 288 Immutability 289 Instance Level Access of Class Level Fields 290 Singleton Destruction 291 Abrupt Shut Down of Daemon Threads 291 Bring Back the stop suspend and resume Methods 292 Blocking I O Should Work Correctly 292 The ThreadGroup Class 292 Wrapping Up 293 List of Figures 294 Chapter 1 The Architecture of Threads 294 Chapter 2 The Perils of Multithreaded Programming 294 Chapter 5 Timers Alarms and Swing Thread Safety 294 Chapter 6 Observers and Multicasters 294 Chapter 9 Object Oriented Threading Architectures 294 List of Tables 294 Chapter 5 Timers Alarms and Swing Thread Safety 294 List of Listings 294 Chapter 1 The Architecture of Threads 294 Chapter 2 The Perils of Multithreaded Programming 295 Chapter 3 The Mutex and Lock Management 295 Chapter 4 Condition Variables and Counting Semaphores 295 Chapter 5 Timers Alarms and Swing Thread Safety 295 Chapter 6 Observers and Multicasters 295 Chapter 7 Singletons Critical Sections and Reader Writer Locks 296 Chapter 8 Threads in an Object Oriented World 296 Chapter 9 Object Oriented Threading Architectures 296 List of Sidebars 297 Chapter 1 The Architecture of Threads 297 Chapter 5 Timers Alarms and Swing Thread Safety 297 7 Taming Java Threads ALLEN HOLUB Apress Copyright 2000 by Allen I Holub All rights reserved No part of this work maybe reproduced or transmitted in any form or by any means electronic or mechanical including photocopying recording or by any information storage or retrieval system without the prior written permission of the copyright owner and the publisher ISBN pbk 1 893115 10 0 Printed and bound in the United States of America 12345678910 Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name we use the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark Copy Editor Tom Robinson TSI Graphics Artist Warren Fischbach TSI Graphics Compositor and Pretty Good Vorpal Player Susan Glinert Indexer Nancy Guenther Project Manager Grace Wong Cover and Interior Design Derek Yee Design Distributed to the book trade worldwide by Springer Verlag New York Inc 175 Fifth Avenue New York NY 10010 In the United States phone 1 800 SPRINGER www springer For information on translations please contact Apress directly Apress 901 Grayson Street Suite 204 Berkeley CA 94710 Phone 510 549 5930 Fax 510 549 5939 The information in this book is distributed on an As Is basis without warranty Although every precaution has been taken in the preparation of this work neither the author nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work To Deirdre Philip and Amanda 8 Preface PROGRAMMING JAVA THREADS is not nearly as easy or as platform independent as most books would have you believe Threading also happens to be an essential part of Java programming All Java programs that use a graphical user interface must be multithreaded for example Threading though is tricky to do right and it usually has to be built into your code from Day 1 This book discusses the things you need to know to program threads effectively in the real world focusing on the legion of problems that arise when you try to use Java s built in threading support The first couple chapters look at threading related problems that are created by the underlying operating environments and then at threading problems such as deadlock that are inherent to all multithreaded systems The middle chapters present a host of classes that implement threading primitives various semaphores timers and so forth that you can use to solve the problems created by Java s less than ideal threading architecture Chapters 8 and 9 focus on threading in object oriented systems and present architectural solutions to threading problems that tend to work better in some situations than the more atomic solutions presented in earlier chapters The final chapter proposes a few modifications to the Java language to clean up the problems discussed in the rest of the book When you re done you ll have a complete toolkit of thread management classes and architectures that you can apply to most of the threading issues that will arise in your programs You ll also understand the Java threading model thoroughly This book is based on a series of nine articles that originally appeared in my Java Toolbox column from the on line magazine Javaworld The code has been reworked considerably and I ve also added quite a bit of material so you should consider this book to be ver 2 of the original series Prerequisites This is not a book for dummies or idiots This book is for intermediate to advanced Java programmers people who know the basics of the language and want to program at an expert level I m assuming that you ve already read and assimilated a reasonable introduction to Java book In particular I assume that you know the syntax of the Java programming language including inner classes anonymous and otherwise know how to create threads using wait and wait know how wait wait and wait work in a general way have a reading level familiarity with the methods of the wait class know the basics of the AWT Swing listener event model If that s not the case you should put this book down and read a good intro to Java book If you know C David Flanagan s Java in a Nutshell presents the differences between Java and C in a very condensed format that I happen to like Non C programmers and those of you who want to take things more slowly will be better served by Peter van der Linden s Just Java 2 4th Edition Getting and Using the Code 9 The code presented in this book is available on line at The version on the Web site is kept up to date and incorporates fixes for the inevitable bugs If you find any bugs by the way please report them to me at The code in this book is not in the public domain Nonetheless you may use it in your programs if you put the following notice in your about box or sign on screen or in your documentation if you don t have an about box or sign on screen This program includes code from Allen Holub s book Taming Java Threads 2000 Allen I Holub All rights reserved Since I don t want hacked up versions floating around on the Net bearing my name you may not redistribute the source code If you ve read my original series of articles in Javaworld the code in the current volume differs a bit from that published in the original series I m sorry if these changes cause any problems for those of you who are using the earlier implementation but the current version is simply better than the old one It is probably worth your while to upgrade I don t plan any harmful changes to the interfaces in the current implementation I might add methods here and there but I won t modify the signatures of or remove existing methods It should be safe to use this code as it evolves without having to change your own programs Allen Holub 10 Chapter 1 The Architecture of Threads PROGRAMMING JAVA THREADS is not nearly as easy or platform independent as most books would have you believe and all Java programs that display a graphical user interface must be multithreaded This chapter shows you why these statements are true by discussing the architectures of various threading systems and by discussing how those architectures influence how you program threads in Java Along the way I ll introduce several key terms and concepts that are not described very well in most intro to Java books Understanding these concepts is essential if you expect to understand the code in the remainder of the book The Problems with Threads Burying your head in the sand and pretending that you don t have to worry about threading issues is a tempting strategy when writing a Java program but you can t usually get away with it in real production code Unfortunately virtually none of the books on Java address threading issues in sufficient depth If anything the books go to the opposite extreme presenting examples that are guaranteed to cause problems in a multithreaded environment as if the code is flawless In fact multithreading is a problem that infuses all your Java code because you have no way of predicting in exactly what context a particular object or method will be used Going back after the fact and trying to make non thread safe code work in a multithreaded environment is an immensely difficult task It s best to start out thinking threads even if you don t plan to use the code you re writing in a multithreaded way in the future Unfortunately there is often a performance penalty to be paid for thread safety so I can t recommend that all code should be thread safe because paying the penalty can just be too high in some situations Nonetheless you should always consider the threading issues when designing the code even if you end up consciously rejecting thread safety in the implementation All Nontrivial Java Programs Are Multithreaded All Java programs other than simple console based applications are multithreaded whether you like it or not The problem is in Java s Abstract Windowing Toolkit AWT Throughout this book I ll use AWT to mean both the 1 1 AWT library and and the Swing extensions to AWT as well AWT processes operating system events on a special thread created by AWT when a program realizes makes visible its first window As a consequence most programs have at least two threads running the main thread on which main executes and the AWT thread which processes events that come in from the operating system and calls any registered listeners in response to those events It s important to note that all your listener methods run on the AWT thread not on the main thread where the listener object is typically created There are two main difficulties to this architecture First although the listeners run on the AWT thread they are typically inner class objects that access an outerclass object that was in turn created by and is accessed by the main thread Put another way listener methods running on AWT thread often access an object that is also manipulated from the main thread the outer class object This is a worst case synchronization problem when two threads compete for access to the same object Proper use of synchronized is essential to force the two threads to take turns accessing the object rather than trying to access it simultaneously To make matters worse the AWT thread that handles the listeners also handles events coming in from the operating system This means that if your listener methods spend a long time doing whatever they do OS level events such as mouse clicks and key presses will not be serviced by your program These events are queued up waiting for service but they are effectively ignored until the listener method returns The result is an unresponsive user interface one that appears to hang It s immensely frustrating to a user when a program ignores clicks on a Cancel button because the AWT thread has 11 called a listener method that takes forever to execute The mouse clicks are ignored until the listener method finishes executing Listing 1 1 demonstrates the unresponsive UI problem This program creates a frame that holds two buttons labeled Sleep and Hello The handler for the Sleep button puts the current thread which will be the Swing event handler thread to sleep for five seconds The Hello button just prints Hello world on the console During the five seconds that elapse after you press the Sleep button pressing the Hello button has no effect If you click the Hello button five times Hello world is printed five times as soon as the sleep finishes The button press events are queued up while the Swing thread is sleeping and they are serviced when the Swing thread wakes up Listing 1 1 text books threads ch1 Hang java 01 import javax swing 02 import java awt 03 import java awt event 04 05 class Hang extends JFrame 06 07 public Hang 08 JButton b1 new JButton Sleep 09 JButton b2 new JButton Hello 10 11 b1 addActionListener 12 new ActionListener 13 public void actionPerformed ActionEvent event 14 try 15 Thread currentThread sleep 5000 16 17 catch Exception e 18 19 20 21 22 b2 addActionListener 23 new ActionListener 24 public void actionPerformed ActionEvent event 25 System out println Hello world 26 27 28 29 30 getContentPane setLayout new FlowLayout 31 getContentPane add b1 12 32 getContentPane add b2 33 pack 34 show 35 36 37 public static void main String args 38 new Hang 39 40 Many books that discuss java GUI building gloss over both the synchronization and the unresponsive UI problems They can get away with ignoring synchronization issues because the trivial examples in those books are often single threaded That is 100 of the code in the program is defined inside one or more listeners all of which are executed serially on the single AWT thread Moreover the listeners perform trivial tasks that complete so quickly that you don t notice that the UI isn t responding In any event in the real world this single threaded approach doing everything on the AWT thread just doesn t work All successful UIs have a few behaviors in common The UI must give you some feedback as an operation progresses Simply throwing up a box that says doing such and such is not sufficient You need to tell the user that progress is being made a percent complete progress bar is an example of this sort of behavior It must be possible to update a window without redrawing the whole thing when the state of the underlying system changes You must provide a way to cancel an in progress operation It must be possible to switch windows and otherwise manipulate the user interface when a long operation is in progress These three rules can be summed up with one rule It s not okay to have an unresponsive UI It s not okay to ignore mouse clicks key presses and so forth when the program is executing a listener method and it s not okay to do lots of time consuming work in listeners The only way to get the reasonable behavior I just described is to use threads Time consuming operations must be performed on background threads for example Real programs will have many more than two threads running at any given moment Java s Thread Support Is Not Platform Independent Unfortunately though it s essential to design with threading issues in mind threads are one of the main places where Java s promise of platform independence falls flat on its face This fact complicates the implementation of platform independent multithreaded systems considerably You have to know something about the possible run time environments to make the program work correctly in all of them It is possible to write a platform independent multithreaded Java program but you have to do it with your eyes open This lamentable situation is not really Java s fault it s almost impossible to write a truly platform independent threading system Doug Schmidt s Ace Framework is a good though complex attempt You can get more information at http www cs wustl edu schmidt ACE html So before I can talk about h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值