Java Threads

Front Cover
"O'Reilly Media, Inc.", 1999 - Computers - 319 pages
0 Reviews

Threads aren't a new idea: many operating systems and languages support them. But despite widespread support, threads tend to be something that everyone talks about, but few use. Programming with threads has a reputation for being tricky and nonportable.

Not so with Java. Java's thread facilities are easy to use, and--like everything else in Java--are completely portable between platforms. And that's a good thing, because it's impossible to write anything but the simplest applet without encountering threads. If you want to work with Java, you have to learn about threads.

This new edition shows you how to take full advantage of Java's thread facilities: where to use threads to increase efficiency, how to use them effectively, and how to avoid common mistakes.

Java Threadsdiscusses problems like deadlock, race condition, and starvation in detail, helping you to write code without hidden bugs. It brings you up to date with the latest changes in the thread interface for JDK 1.2.

The book offers a thorough discussion of the Thread and ThreadGroup classes, the Runnable interface, the language's synchronized operator. It explains thread scheduling ends by developing a CPUSchedule class, showing you how to implement your own scheduling policy. In addition,Java Threadsshows you how to extend Java's thread primitives. Other extended examples include classes that implement reader/writer locks, general locks, locks at arbitrary scope, and asynchronous I/O. This edition also adds extensive examples on thread pools, advanced synchronization technique, like condition variables, barriers, and daemon locks. It shows how to work with classes that are not thread safe, and pays special attention to threading issues with Swing. A new chapter shows you how to write parallel code for multiprocessor machines.

In short,Java Threadscovers everything you need to know about threads, from the simplest animation applet to the most complex applications. If you plan to do any serious work in Java, you will find this book invaluable. Examples available online. Covers Java 2.

 

What people are saying - Write a review

We haven't found any reviews in the usual places.

Selected pages

Contents

Introduction to Threading
1
Thread Overview
3
Why Threads?
6
Summary
11
The Java Threading API
12
Threading Using the Runnable Interface
19
The Life Cycle of a Thread
24
Thread Naming
28
An Overview of Thread Scheduling
124
When Scheduling Is Important
135
Scheduling with Thread Priorities
138
Popular Scheduling Implementations
142
Native Scheduling Support
153
Other ThreadScheduling Methods
157
Summary
167
Java Thread Scheduling Examples
169

Thread Access
30
More on Starting Stopping and Joining
34
Summary
37
Synchronization Techniques
40
Reading Data Asynchronously
44
A Class to Perform Synchronization
49
The Synchronized Block
53
Nested Locks
55
Deadlock
58
Return to the Banking Example
61
Synchronizing Static Methods
63
Summary
65
Wait and Notify
67
Wait and Notify
68
wait notify and notifyAll
74
wait and sleep
77
Thread Interruption
79
Static Methods Synchronization Details
85
Summary
86
Useful Examples of Java Thread Programming
88
Simple Synchronization Examples
93
A Network Server Class
101
The AsyncInputStream Class
108
Using TCPServer with AsyncInputStreams
121
Summary
122
Java Thread Scheduling
123
Thread Pools
170
RoundRobin Scheduling
176
Job Scheduling
191
Summary
197
Advanced Synchronization Topics
198
Preventing Deadlock
200
Lock Starvation
208
ThreadUnsafe Classes
222
Summary
234
Parallelizing for Multiprocessor Machines
235
Parallelizing a SingleThreaded Program
236
InnerLoop Threading
255
Loop Printing
259
Multiprocessor Scaling
263
Summary
273
Thread Groups
274
Creating Thread Groups
275
Thread Group Methods
278
Manipulating Thread Groups
283
Thread Groups Threads and Security
285
Summary
291
Miscellaneous Topics
295
Exceptions and Errors
304
Index
311
Copyright

Common terms and phrases

References to this book

All Book Search results »

About the author (1999)

Scott Oaks is a Java Technologist at Sun Microsystems, where he has worked since 1987. While at Sun, he has specialized in many disparate technologies, from the SunOS kernel to network programming and RPCs. Since 1995, hes focused primarily on Java and bringing Java technology to end-users. Scott also authored OReillys Java Security, Java Threads and Jini in a Nutshell titles.

Henry Wong is a tactical engineer at Sun Microsystems, where he has worked since 1989. Originally hired as a consultant to help customers with special device drivers, kernel modifications, and DOS interoperability products, Henry has also worked on Solaris ports, performance tuning projects, and multithreaded design and implementations for benchmarks and demos. Since early 1995, Henry has been involved in developing Java prototypes and supporting customers who are using Java. Prior to joining Sun, Henry earned a bachelor of science degree in chemical engineering from The Cooper Union in 1987. He joined a small software company in 1986 working on SCSI device drivers, image and audio data compression, and graphics tools used for a medical information system. When not in front of a computer, Henry is an instrument rated private pilot, who also likes to dabble in archery, cooking, and traveling to different places with his wife, Nini.

Bibliographic information