did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

did-you-know? rent-now

Amazon no longer offers textbook rentals. We do!

We're the #1 textbook rental company. Let us show you why.

9780130170071

Multithreaded Programming with JAVA technology

by ; ;
  • ISBN13:

    9780130170071

  • ISBN10:

    0130170070

  • Edition: 1st
  • Format: Paperback
  • Copyright: 1999-12-17
  • Publisher: Prentice Hall
  • Purchase Benefits
  • Free Shipping Icon Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • eCampus.com Logo Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $49.99

Summary

Preface

Today, there are three primary sets of multithreading (MT) libraries: the POSIX threads library, the Win32 threads library (both native), and Java. Although the APIs and implementations differ significantly, the fundamental concepts are the same. The ideas in this book are valid for all three; the details of the APIs differ.

All the specific discussion in this book focuses on the Java multithreading model, with comparisons to POSIX and Win32 throughout. Java threads are always implemented upon a low—level library which does the real work. Hence Java on UNIX is generally based on POSIX, while Java on NT will be based on Win32 threads.

Because these lower—level libraries have so much impact on the actual performance of a Java program, we will devote significant attention to the native libraries. Because POSIX threads are more primitive than Win32 threads, they will be our basis of comparison and explanation. This allows us to explain the inner workings of threads before jumping to the more intricate workings of Java.

A frank note about our motivation is in order here. We have slaved away for countless hours on this book because we're propeller—heads who honestly believe that this technology is a superb thing and that the widespread use of it will make the world a better place for hackers like ourselves.

Your motivations for writing MT programs? You can write your programs better and more easily, they'll run faster, you'll get them to market more quickly, they'll have fewer bugs, and you'll have happier programmers, customers, and higher sales. The only losers in this game are the competitors, who will lag behind you in application speed and quality.

MT is here today. It is now ubiquitous. As a professional programmer, you have an obligation to understand this technology. It may or may not be appropriate for your current project, but you must be able to make that conclusion yourself. This book will give you what you need to make that decision.

Welcome to the world of the future!

Who Should Use This Book

This book aims to give the programmer or technical manager a solid understanding of threads—what they are, how they work, why they are useful, and some of the programming issues surrounding their use. As an introductory text, it does not attempt a deep, detailed analysis of the most current research, but it does come close. After reading this book the reader should have a solid understanding of the fundamentals, be able to write credible, modestly complex, threaded programs, and have the understanding necessary to analyze their own programs and determine the viability of threading them.

This book has been written with the experienced Java programmer in mind. There is a definite UNIX bias, but none of that is essential to understanding. A Java programmer who does not know C will find the POSIX code fragments mildly challenging, although possible to decipher. The concepts should be clear. A technically minded nonprogrammer should be able to follow most of the concepts and understand the value of threads. A nontechnical person will not get much from this book.

This book does not attempt to explain the use of Win32 or POSIX APIs. It does contrast them to Java APIs to explain some of the higher—level Java behavior in lower—level terms.

How This Book Is Organized

Chapter 1, Introduction —In which we discuss the motivation for creating thread libraries, the advent of shared memory multiprocessors, and the interactions between threads and SMP machines.

Chapter 2, Concepts —In which the reader is introduced to the basic concepts of multitasking operating systems and of multithreading as it compares to other programming paradigms. The reader is shown reasons why multithreading is a valuable addition to programming paradigms, and a number of examples of successful deployment are presented.

Chapter 3, Foundations —In which we introduce the reader to the underlying structures upon which threads are built, the construction of the thread itself, and the operating system support that allows efficient implementation.

Chapter 4, Lifecycle —In which the reader is treated to a comprehensive explanation of the intricacies in the life of a thread—birth, life, and death—even death by vile cancellation. A small program that illustrates all these stages concludes the chapter.

Chapter 5, Scheduling —In which we explain the myriad details of various scheduling models and alternative choices that could be made, describe context switching in detail, and delve into gruesome detail on various design options. There is light at the end of the tunnel, however.

Chapter 6, Synchronization —In which the reader is led on a hunt for the intimidating synchronization variable and discovers that it is not actually as frightening as had been thought. Programs illustrating the basic use of the POSIX and Java primitives are shown.

Chapter 7, Complexities —In which a series of more complex synchronization variables and options are presented and the trade—off between them and the simpler ones are discussed. Synchronization problems and techniques for dealing with them conclude the chapter.

Chapter 8, TSD —In which explanations of thread—specific data, their use, and some implementation details are provided.

Chapter 9, Cancellation —In which we describe the acrimonious nature of some programs and how unwanted threads may be disposed of. The highly complex issues surrounding bounded time termination and program correctness are also covered. A simple conclusion is drawn.

Chapter 10, Details —In which a number of minor details are covered.

Chapter 11, Libraries —In which we explore a variety of operating systems issues that bear heavily upon the usability of threads in actual programs. We examine the status of library functions and the programming issues facing them. We look at some design alternatives for library functions.

Chapter 12, Design —In which we explore some designs for programs and library functions. Making both programs and individual functions more concurrent is a major issue in the design of these functions. We look at a variety of code examples and the trade—offs between them.

Chapter 13, RMI —In which we examine RMI and see what it provides in terms of a distributed object programming model. We look at how threading interacts with it and how it uses threads.

Chapter 14, Tools —In which we consider the kinds of new tools that a reader would want when writing a threaded program. An overview of the Solaris tool set is given, as representative of what should be looked for.

Chapter 15, Performance —In which we make things faster, look at general performance issues, political performance issues, and thread specific performance issues. We conclude with a discussion of the actual performance of multithreaded NFS.

Chapter 16, Hardware —In which we look at the various designs for SMP machines (cache architectures, interconnect topologies, atomic instructions, invalidation techniques) and consider how those designs affect our programming decisions. Some optimization possibilities are looked at.

Chapter 17, Examples —In which several complete programs are presented. The details and issues surrounding the way they use threads are discussed, and references to other programs on the Net are made.

Author Biography

Daniel J. Berg is a Tactical Engineer for Sun Microsystems Reseller Area, specializing in multithreaded applications, object and distributed technologies, and Java technology.

Table of Contents

Preface xv
Who Should Use This Book xvi
How This Book Is Organized xvii
Acknowledgments xxi
Acknowledgments to the Threads Primer xxii
Acknowledgments to the Pthreads Primer xxiv
Introduction
1(6)
Concepts
7(26)
Background: Traditional Operating Systems
8(3)
What Is a Thread?
11(3)
Kernel Interaction
14(3)
Concurrency vs. Parallelism
14(1)
System Calls
15(1)
Signals
16(1)
Synchronization
16(1)
Scheduling
17(1)
The Value of Using Threads
17(7)
Parallelism
18(1)
Throughput
19(1)
Responsiveness
19(1)
Communications
20(1)
System Resources
21(1)
Distributed Objects
22(1)
Same Binary for Uniprocessors and Multiprocessors
22(1)
Program Structure
23(1)
What Kinds of Programs to Thread
24(3)
Inherently MT Programs
24(1)
Not Obviously MT Programs
25(1)
Automatic Threading
25(1)
Programs Not to Thread
26(1)
What About Shared Memory?
27(1)
Threads Standards
27(1)
Performance
28(5)
Operating Systems
29(1)
NFS
29(1)
SPECfp 95
30(1)
SPECint_rate95
30(1)
Java Benchmarks
30(3)
Foundations
33(14)
Implementation vs. Specification
34(1)
Thread Libraries
34(2)
The Process Structure
36(1)
Lightweight Processes
37(4)
Threads and LWPs
39(2)
The POSIX Multithreaded Model
41(1)
System Calls
42(3)
Signals
45(2)
Lifecycle
47(24)
Thread Lifecycle
48(16)
Exiting a Thread
49(1)
The Runnable Interface
50(2)
Waiting for Threads
52(1)
Who Am I?
53(1)
Exiting the Process
54(1)
Suspending a Thread
54(1)
Cancellation
55(1)
ThreadDeath
56(1)
Garbage Collecting Threads
56(1)
Zombies
57(1)
Is She Still Alive?
57(1)
Restarting Threads
58(1)
An Example: Create and Join
59(5)
APIs Used in This Chapter
64(1)
The Class java.lang.Thread
64(4)
The Class Extensions.Interruptible Thread
68(1)
The Interface java.lang.Runnable
68(3)
Scheduling
71(26)
Different Models of Kernel Scheduling
72(3)
Many Threads on One LWP
72(1)
One Thread per LWP
73(1)
Many Threads on Many LWPs (Strict)
74(1)
The Two-Level Model
74(1)
Thread Scheduling
75(7)
Process Contention Scope
77(5)
System Contention Scope
82(1)
Context Switching
82(10)
Preemption
86(1)
How Many LWPs?
87(1)
How to Get Those LWPs in Java
87(2)
Changing Scheduling Parameters for LWPs
89(1)
Realtime LWPs
89(1)
Allocation Domains
90(1)
Binding LWPs to Processors
90(2)
Java Scheduling Summary
92(1)
When Should You Care About Scheduling?
93(1)
APIs Used in This Chapter
94(1)
The Class Java.lang.Thread
94(3)
Synchronization
97(42)
Synchronization Issues
98(2)
Atomic Actions and Atomic Instructions
98(1)
Critical Sections
99(1)
Lock Your Shared Data!
100(1)
Synchronization Variables
100(34)
Mutexes
101(9)
Semaphores
110(6)
Condition Variables
116(3)
Java wait/notify
119(2)
InterruptedException
121(2)
Controlling the Queue Length
123(2)
POSIX-Style Synchronization in Java
125(9)
APIs Used in This Chapter
134(1)
The Class java.Iang.Object
134(1)
The Class Extensions.Semaphore
135(1)
The Class Extensions.Mutex
136(1)
The Class Extensions.ConditionVar
136(3)
Complexities
139(38)
Complex Locking Primitives
140(10)
Readers/Writer Locks
140(4)
Priority Inheritance Mutexes
144(1)
FIFO Mutexes
145(2)
Recursive Mutexes
147(1)
Nonblocking Synchronization
148(1)
Spin Locks
148(2)
Timeouts
150(2)
Elvis and the UFOs
151(1)
Other Synchronization Variables
152(7)
Join
152(1)
Barriers
153(1)
Single Barriers
154(2)
Win32 Event Objects
156(1)
Win32 Critical Sections
157(1)
Multiple Wait Semaphores
157(1)
Interlocked Instructions
157(1)
Message Queues
158(1)
Win32 I/O Completion Ports
158(1)
Communicating via Streams
159(1)
Volatile
159(1)
Performance
160(5)
Condition Variables vs. wait/notify
160(2)
Coarse vs. Fine Grain Locking
162(1)
What to Lock
162(2)
Double-Checked Locking
164(1)
Synchronization Problems
165(8)
Deadlocks
166(2)
Race Conditions
168(1)
Recovering from Deadlocks
168(2)
The Lost Wakeup
170(1)
InterruptedException
171(2)
APIs Used in This Chapter
173(1)
The Class Extensions.RWLock
173(1)
The Class Extensions.Barrier
174(1)
The Class Extensions.SingleBarrier
174(3)
TSD
177(8)
Thread-Specific Data
178(2)
Java TSD
180(3)
APIs Used in This Chapter
183(1)
The Class java.Iang.ThreadLocal
183(2)
Cancellation
185(38)
What Cancellation Is
186(3)
Polling for Cancellation
186(1)
Asynchronous Cancellation
187(1)
Deferred Cancellation
188(1)
Using interrupt() for Deferred Cancellation
188(1)
Progressive Shutdown
188(1)
interrupt()
189(12)
Don't Call stop()
190(1)
ThreadDeath
191(1)
Using stop() to Implement Thread.exit()
191(1)
Never Exit a Thread!
192(1)
Defined Cancellation/Interruption Points
193(1)
Not Cancelling upon Interruption
194(1)
Handling Interrupts
194(6)
Cancellation State
200(1)
A Cancellation Example
201(6)
Using Cancellation
207(7)
Ensuring Bounded CPU Time
208(4)
Interrupting Sleeping Threads
212(1)
The Morning After
213(1)
Cleanup
214(1)
Implementing enableInterrupts()
215(2)
A Cancellation Example (Improved)
217(1)
Simple Polling
218(1)
APIs Used in This Chapter
219(1)
The Class java.Iang.Thread
219(1)
The Class Extensions.Interruptible Thread
220(3)
Details
223(26)
Thread Groups
224(1)
Thread Security
224(10)
Real-World Examples
230(4)
General Tips and Hints
234(1)
Daemon Threads
234(1)
Daemon Threads Groups
235(1)
Calling Native Code
235(3)
A Few Assorted Methods
238(1)
Stack Size
238(1)
Deprecated Methods
239(1)
The Effect of Using a JIT
239(1)
Adaptive Compilers
239(1)
APIs Used in This Chapter
240(1)
The Class java.lang.Thread
240(2)
The Class java.lang.ThreadGroup
242(7)
Libraries
249(14)
The Native Threads Libraries
250(1)
Multithreaded Kernels
250(3)
Symmetric Multiprocessing
252(1)
Are Libraries Safe?
253(7)
Window Systems
254(4)
Working with Unsafe Libraries
258(1)
When Should a Class Be Synchronized?
259(1)
Synchronized Collections in Java 2
259(1)
Java's Multithreaded Garbage Collector
260(3)
Design
263(24)
Making Libraries Safe and Hot
264(6)
Making malloc() More Concurrent
267(3)
Manipulating Lists
270(9)
Single, Global Mutex
271(2)
Global RWLock with Global Mutex to Protect Salaries
273(2)
Global RWLock with Local Mutex to Protect Salaries
275(1)
One Local Lock
276(2)
Two Local Locks
278(1)
Local RWLock with Local Mutex to Protect Salaries
279(1)
Program Design
279(6)
Design Patterns
285(2)
RMI
287(14)
Remote Method Invocation
288(13)
Sending Remote References
290(7)
RMI's Use of Threads
297(1)
The Deadlock Problem with RMI
298(1)
Remote Garbage Collection
299(2)
Tools
301(12)
Static Lock Analyzer
302(1)
Using a Thread-Aware, Graphical Debugger
302(3)
Proctool
305(1)
TNFview
306(7)
Performance
313(28)
Optimization: Objectives and Objections
314(3)
CPU Time, I/O Time, Contention, Etc.
317(4)
CPU
317(1)
Memory Latency
318(1)
Memory Bandwidth
318(1)
I/O Latency
319(1)
Contention
319(1)
Throughput vs. Latency
320(1)
Limits on Speedup
321(5)
Amdahl's Law
323(1)
Performance Bottlenecks
324(2)
Benchmarks and Repeatable Testing
326(10)
Is It Really Faster?
327(2)
General Performance Optimizations
329(3)
Thread-Specific Performance Optimizations
332(3)
Dealing with Many Open Sockets
335(1)
The Lessons of NFS
336(5)
Hardware
341(24)
Types of Multiprocessors
342(4)
Shared Memory Symmetric Multiprocessors
342(4)
Bus Architectures
346(12)
LoadLocked/StoreConditional and Compare and Swap
354(3)
Volatile: The Rest of the Story
357(1)
Memory Systems
358(7)
Reducing Cache Misses
359(6)
Examples
365(44)
Threads and Windows
366(6)
Displaying Things for a Moment (Memory.java)
372(2)
Socket Server (Master/Slave Version)
374(1)
Socket Server (Producer/Consumer Version)
375(10)
Making a Native Call to pthread_setconcurrency()
385(1)
Actual Implementation of POSIX Synchronization
386(3)
A Robust, Interruptible Server
389(13)
Disk Performance with Java
402(6)
Other Programs on the Web
408(1)
Appendix A Internet 409(4)
Threads Newsgroup
410(1)
Code Examples
410(1)
Vendor's Threads Pages
410(1)
Threads Research
411(1)
Freeware Tools
411(1)
Other Pointers
412(1)
The Authors on the Net
412(1)
Appendix B Books 413(6)
Threads Books
414(2)
Related Books
416(3)
Appendix C Timings 419(6)
Appendix D APIs 425(24)
Function Descriptions
426(1)
The Class java.Iang.Thread
426(7)
The Interface java.lang.Runnable
433(1)
The Class java.lang.Object
434(1)
The Class java.lang.ThreadLocal
435(1)
The Class java.lang.ThreadGroup
435(6)
Helper Classes from Our Extensions Library
441(1)
The Class Extensions.Interruptible Thread
441(1)
The Class Extensions.Semaphore
442(1)
The Class Extensions.Mutex
443(1)
The Class Extensions.ConditionVar
444(1)
The Class Extensions.RWLock
444(1)
The Class Extensions.Barrier
445(1)
The Class Extensions.SingleBarrier
446(3)
Glossary 449(8)
Index 457

Supplemental Materials

What is included with this book?

The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.

The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.

Excerpts

PrefaceToday, there are three primary sets of multithreading (MT) libraries: the POSIX threads library, the Win32 threads library (both native), and Java. Although the APIs and implementations differ significantly, the fundamental concepts are the same. Theideasin this book are valid for all three; the details of the APIs differ.All the specific discussion in this book focuses on the Java multithreading model, with comparisons to POSIX and Win32 throughout. Java threads are always implemented upon a low--level library which does the real work. Hence Java on UNIX is generally based on POSIX, while Java on NT will be based on Win32 threads.Because these lower--level libraries have so much impact on the actual performance of a Java program, we will devote significant attention to the native libraries. Because POSIX threads are more primitive than Win32 threads, they will be our basis of comparison and explanation. This allows us to explain the inner workings of threads before jumping to the more intricate workings of Java.A frank note about our motivation is in order here. We have slaved away for countless hours on this book because we're propeller--heads who honestly believe that this technology is a superb thing and that the widespread use of it will make the world a better place for hackers like ourselves.Your motivations for writing MT programs? You can write your programs better and more easily, they'll run faster, you'll get them to market more quickly, they'll have fewer bugs, and you'll have happier programmers, customers, and higher sales. The only losers in this game are the competitors, who will lag behind you in application speed and quality.MT is here today. It is now ubiquitous. As a professional programmer, you have an obligation to understand this technology. It may or may not be appropriate for your current project, but you must be able to make that conclusion yourself. This book will give you what you need to make that decision.Welcome to the world of the future! Who Should Use This BookThis book aims to give the programmer or technical manager a solid understanding of threads--what they are, how they work, why they are useful, and some of the programming issues surrounding their use. As an introductory text, it does not attempt a deep, detailed analysis of the most current research, but it does come close. After reading this book the reader should have a solid understanding of the fundamentals, be able to write credible, modestly complex, threaded programs, and have the understanding necessary to analyze their own programs and determine the viability of threading them.This book has been written with the experienced Java programmer in mind. There is a definite UNIX bias, but none of that is essential to understanding. A Java programmer who does not know C will find the POSIX code fragments mildly challenging, although possible to decipher. The concepts should be clear. A technically minded nonprogrammer should be able to follow most of the concepts and understand the value of threads. A nontechnical person will not get much from this book.This book does not attempt to explain the use of Win32 or POSIX APIs. It does contrast them to Java APIs to explain some of the higher--level Java behavior in lower--level terms. How This Book Is OrganizedChapter 1,Introduction--In which we discuss the motivation for creating thread libraries, the advent of shared memory multiprocessors, and the interactions between threads and SMP machines.Chapter 2,Concepts--In which the reader is introduced to the basic concepts of multitasking operating systems and of multithreading as it compares to other programming paradigms. The reader is shown reasons why multithreading is a valuable addition to programming paradigms, and a numbe

Rewards Program