Java Thread Programming

Java Thread Programming

by Paul Hyde

Paperback

$39.99

Product Details

ISBN-13: 9780672315855
Publisher: Sams
Publication date: 09/03/1999
Series: Sams White Book Series
Pages: 400
Product dimensions: 7.00(w) x 8.90(h) x 1.10(d)

About the Author

Paul Hyde is a professional Java developer and Sun Certified Java Programmer who has over 3,200 hours of on-the-job Java development experience. He speaks at Java conferences about Java threads, and is currently a Java consultant for Programmix Incorporated. Mr. Hyde has developed introductory and advanced Java courses for IT and programming professionals, and has taught classes for Sybase.

Table of Contents

Introduction1
Part 1Threads5
1Introduction to Threads7
What Is a Thread?8
Why Use Multiple Threads?9
When Multiple Threads Might Not Be Good12
Java's Built-in Thread Support12
Basy to Start, Tough to Master13
2A Simple Two-Thread Example15
Extending the java.lang.Thread Class16
Overriding the run() Method18
Spawning a New Thread19
Putting It All Together20
Summary22
3Crealing and Starting a Thread23
Using Thread.currentThread()24
Naming a Thread: getName() and setName()27
Thread Constructors31
Enlivening a Thread: start() and isAlive()33
Using Thread.sleep()35
Summary38
4Implementing Runnable Versus Extending Thread39
Visual Timer Graphical Component40
Extending Thread and JComponent?46
Interface java.lang.Runnable47
Passing a Runnable Object to a Thread's Constructor48
Modifying SecondCounter to Use Runnable49
Checking the Accuracy of SecondCounter55
Improving the Accuracy of SecondCounter59
Summary64
5Gracefully Stopping Threads65
Interrupting a Thread: interrupt()66
Suspending and Resuming Thread Execution74
Stopping a Thread86
The Best Replacement for stop(), suspend(), and resume()91
Daemon Threads97
Summary99
6Thread Prioritization101
System Thread Priorities102
Thread Priority Constants103
Determining the Current Priority: getPriority()104
Changing the Priority of a Thread: setPriority()106
Thread States109
Priorities and Scheduling111
Voluntarily Relinquishing the Processor: Thread.yield()112
Thread-Scheduling Scenarios117
Summary119
7Concurrent Access to Objects and Variables121
Volatile Member Variable Modifier122
Synchronized Method Modifier128
Synchronized Statement Block144
Static synchronized Methods148
Using the Class-Level Lock in a synchronized Statement153
Synchronization and the Collections API155
Deadlocks160
Speeding Concurrent Access165
Summary165
8Inter-thread Communication167
The Need for Inter-thread Signaling168
The Wait/Notify Mechanism168
Object API Used for Wait/Notify173
When to Use notify All() Instead of notify()175
Missed Notification176
Early Notification184
CubbyHole Example192
Using join() to Wait for a Thread to Die198
Streaming Data Between Threads Using Pipes202
Using ThreadLocal and Inheritable ThreadLocal209
Summary220
9Threads and Swing221
Why Isn't the Swing Toolkit Multithread-Safe?222
Using Swing Utilities.invokeAndWait()223
Using Swing Utilities.invokeLater()227
Using SwingUtilities.isEventDispatch Thread()230
When invokeAndWait() and invokeLater() Are Not Needed231
The Need for Worker Threads in a GUI Setting231
Using a Worker Thread to Relieve the Event Thread236
Scrolling Text in a Custom Component244
Animating a Set of Images249
Displaying Elapsed Time on a JLabel254
Floating Components Around Inside a Container257
Summary263
10Thread Groups265
What Are Thread Groups?266
Using getParent()267
Finding the Subgroups of a Thread Group268
Using the getThreadGroup() Method of Thread268
Finding All the Threads in a Thread Group269
Understanding Thread Group Security269
Using setMaxPriority() and getMaxPriority()270
Using interrupt()270
Deprecated Methods: stop(), suspend(), and resume()270
Class ThreadViewer270
Summary279
Part 2Techniques281
11Self-Running Objects283
Simple Self-Running Class284
Using an Inner Class to Hide run()287
Additional Functionality to Consider289
Summary296
12Exception Callback297
ExceptionListener Interface298
Additional Methods to Support ExceptionListener298
Summary306
13Thread Pooling307
Benefits of Thread Pooling308
Considerations and Costs of Thread Pooling308
A Generic Thread Pool: ThreadPool309
A Specialized Worker Thread Pool: HttpServer319
Summary340
14Waiting for the Full Timeout341
Accidentally Returning Early342
Determining If wait() Should Be Invoked Again345
A General-Purpose Wait-Until Pattern349
Summary355
15Breaking Out of a Blocked I/O State357
The read() Method Ignores Interrupts and Stop Requests358
Closing a Stream to Break Out of the Blocked State361
Throwing InterruptedIOException When Interrupted369
Using BufferedThreadedInputStream for Interruptible I/O380
Summary385
16The SureStop Utility387
Guidelines for Using SureStop388
The SureStop Class389
Peering Inside Using SureStop Verbose395
Watching It Work with SureStopDemo400
Summary405
17The BooleanLock Utility407
Background408
BooleanLock Class408
Inter-Thread Signaling Using BooleanLock412
Avoiding Blocking on synchronized414
Noticing Brief Changes in Value Using TransitionDetector420
Summary427
18First-In-First-Out (FIFO) Queue429
How a FIFO Queue Works430
Implementing a FIFO with an Array431
Simple Implementation in Java: SimpleObjectFIFO434
An Expanded FIFO Queue for Object References: ObjectFIFO441
A FIFO Queue for Bytes: ByteFIFO454
Summary466
Part 3Appendixes467
AThe Thread API469
Member Variables470
Constructors471
Static methods473
Instance Methods474
Deprecated Methods478
BThe ThreadGroup API481
Constructors482
Instance Methods483
Deprecated Methods487
Index489

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews

Java Thread Programming 5 out of 5 based on 0 ratings. 4 reviews.
Guest More than 1 year ago
A book on Java Thread Programming can not be be better than this. Read this book and you will get everything you need to now about java thread programming.
Guest More than 1 year ago
Very good, pragmatic approach to this advanced and difficult topic. Lots of simple code examples which help to get the points accross, plus a few more sophisticated utilities. If you work through it, you have a solid foundation to tackle your own multi threading-problems and may be even have fun with it. And if you know the topic already, you won't be sorry if you buy.
Guest More than 1 year ago
This book is one of the best programming books I've EVER read! Its a must have for Java developers that need to do some deep thread programming or just for the novice programmer that wants to learn thread programming in Java. When I first read this book I knew almost nothing about Thread programming in Java and after reading it I felt like a GURU! This book will take you step by step through this subject with MANY examples. The book is quite clear, but beware it may be a little difficult to newbie java programmers. The reader should be quite familiar with general programming in Java and object oriented programming. In conclusion, I highly recommend this book! In my opinion, it is DEFINATELY a must-have for ALL Java Programmers!!!
Guest More than 1 year ago
Threads are a tricky business and while Java makes their easier there are still lots of pitfalls and gotchas. This is the first book I've found with solid examples that have already help me solve some problems I working on. Other books, while sometimes good (Lea's book comes to mind) are more theory and less example based than I like. This book on the other hand, is full of useful examples. Recommended if you need to learn about the topic.