Home > Store

Multithreaded Programming with Java Technology

Register your product to gain access to bonus material or receive a coupon.

Multithreaded Programming with Java Technology

Book

  • Sorry, this book is no longer in print.
Not for Sale

About

Features

  • Thread scheduling models and synchronization—with solutions for complex, real-world synchronization problems.
  • Multithreaded program design for networked and Internet applications.
  • Thread-specific data: use and implementation.
  • Leveraging OS libraries to make Java-based multithreading more effective.
  • Optimizing thread performance and designing for SMP hardware.

Description

  • Copyright 2000
  • Dimensions: 7" x 9-1/4"
  • Edition: 1st
  • Book
  • ISBN-10: 0-13-017007-0
  • ISBN-13: 978-0-13-017007-1

Java offers powerful multithreading capabilities -- even on operating systems that offer no inherent multithreading support. Multithreading gives Java developers a powerful tool for dramatically improving the responsiveness and performance of their programs. Now there's a complete guide to multithreaded programming with the Java 2 platform, written by multithreaded programming experts Bil Lewis and Daniel J. Berg. Start by understanding the rationale, fundamental concepts and foundations of multithreading, including the structures upon which threads are built, thread construction and lifecycles. Then, using extensive code examples, you'll master thread scheduling models; synchronization variables; and learn how to solve complex synchronization problems. The authors explain thread-specific data, and demonstrate how to leverage OS libraries to make Java multithreading more effective. They also cover multithreaded program design, interactions with RMI, tools for building threaded programs; and advanced techniques for maximizing thread performance.

Sample Content

Table of Contents



1. Introduction.


2. Concepts.

Background: Traditional Operating Systems. What Is a Thread? Kernel Interaction. Concurrency vs Parallelism. System Calls. Signals. Synchronization. Scheduling. The Value of Using Threads. Parallelism. Throughput. Responsiveness. Communications. System Resources. Distributed Objects. Same Binary for Uniprocessors and Multiprocessors. Program Structure. What Kinds of Programs to Thread. Inherently MT Programs. Not Obviously MT Programs. Automatic Threading. Programs Not to Thread. What About Shared Memory? Threads Standards. Performance. Operating Systems. NFS. SPECfp 95. SPECint_rate95. Java Benchmarks.



3. Foundations.

Implementation vs Specification. Thread Libraries. The Process Structure. Lightweight Processes. Threads and LWPs. The POSIX Multithreaded Model. System Calls. Signals.



4. Lifecycle.

Thread Lifecycle. Exiting a Thread. The Runnable Interface. Waiting for Threads. Who Am I? Exiting the Process. Suspending a Thread. Cancellation. ThreadDeath. Garbage Collecting Threads. Zombies. Is She Still Alive? Restarting Threads. An Example: Create and Join. APIs Used in This Chapter. The Class java.lang.Thread. The Class Extensions.InterruptibleThread. The Interface java.lang.Runnable.



5. Scheduling.

Different Models of Kernel Scheduling. Many Threads on One LWP. One Thread per LWP. Many Threads on Many LWPs (Strict). The Two-Level Model. Thread Scheduling. Process Contention Scope. System Contention Scope. Context Switching. Preemption. How Many LWPs? How to Get Those LWPs in Java. Changing Scheduling Parameters for LWPs. Realtime LWPs. Allocation Domains. Binding LWPs to Processors. Java Scheduling Summary. When Should You Care About Scheduling? APIs Used in This Chapter. The Class java.lang.Thread.



6. Synchronization.

Synchronization Issues. Atomic Actions and Atomic Instructions. Critical Sections. Lock Your Shared Data! Synchronization Variables. Mutexes. Semaphores. Condition Variables. Java wait/notify. InterruptedException. Controlling the Queue Length. POSIX-Style Synchronization in Java. APIs Used in This Chapter. The Class java.lang.Object. The Class Extensions.Semaphore. The Class Extensions.Mutex. The Class Extensions.ConditionVar.



7. Complexities.

Complex Locking Primitives. Readers/Writer Locks. Priority Inheritance Mutexes. FIFO Mutexes. Recursive Mutexes. Nonblocking Synchronization. Spin Locks. Timeouts. Elvis and the UFOs. Other Synchronization Variables. Join. Barriers. Single Barriers. Win32 Event Objects. Win32 Critical Sections. Multiple Wait Semaphores. Interlocked Instructions. Message Queues. Win32 I/O Completion Ports. Communicating via Streams. Volatile. Performance. Condition Variables vs wait/notify. Coarse vs Fine Grain Locking. What to Lock. Double-Checked Locking. Synchronization Problems. Deadlocks. Race Conditions. Recovering from Deadlocks. The Lost Wakeup. InterruptedException. APIs Used in This Chapter. The Class Extensions.RWLock. The Class Extensions.Barrier. The Class Extensions.SingleBarrier.



8. TSD.

Thread-Specific Data. Java TSD. APIs Used in This Chapter. The Class java.lang.ThreadLocal.



9. Cancellation.

What Cancellation Is. Polling for Cancellation. Asynchronous Cancellation. Deferred Cancellation. Using interrupt() for Deferred Cancellation. Progressive Shutdown. interrupt( ). Don't Call stop(). ThreadDeath. Using stop() to Implement Thread.exit(). Never Exit a Thread! Defined Cancellation/Interruption Points. Not Cancelling upon Interruption. Handling Interrupts. Cancellation State. A Cancellation Example. Using Cancellation. Ensuring Bounded CPU Time. Interrupting Sleeping Threads. The Morning After. Cleanup. Implementing enableInterrupts( ). A Cancellation Example (Improved). Simple Polling. APIs Used in This Chapter. The Class java.lang.Thread. The Class Extensions.InterruptibleThread.



10. Details.

Thread Groups. Thread Security. Real-World Examples. General Tips and Hints. Daemon Threads. Daemon Thread Groups. Calling Native Code. A Few Assorted Methods. Stack Size. Deprecated Methods. The Effect of Using a JIT. Adaptive Compilers. APIs Used in This Chapter. The Class java.lang.Thread. The Class java.lang.ThreadGroup.



11. Libraries.

The Native Threads Libraries. Multithreaded Kernels. Symmetric Multiprocessing. Are Libraries Safe? Window Systems. Working with Unsafe Libraries. When Should a Class Be Synchronized? Synchronized Collections in Java 2. Java's Multithreaded Garbage Collector.



12. Design.

Making Libraries Safe and Hot. Making malloc() More Concurrent. Manipulating Lists. Single, Global Mutex. Global RWLock with Global Mutex to Protect Salaries. Global RWLock with Local Mutex to Protect Salaries. One Local Lock. Two Local Locks. Local RWLock with Local Mutex to Protect Salaries. Program Design. Design Patterns.



13. RMI.

Remote Method Invocation. Sending Remote References. RMI's Use of Threads. The Deadlock Problem with RMI. Remote Garbage Collection.



14. Tools.

Static Lock Analyzer. Using a Thread-Aware, Graphical Debugger. Proctool. TNFview.



15. Performance.

Optimization: Objectives and Objections. CPU Time, I/O Time, Contention, Etc. CPU. Memory Latency. Memory Bandwidth. I/O Latency. Contention. Throughput vs Latency. Limits on Speedup. Amdahl's Law. Performance Bottlenecks. Benchmarks and Repeatable Testing. Is It Really Faster? General Performance Optimizations. Thread-Specific Performance Optimizations. Dealing with Many Open Sockets. The Lessons of NFS.



16. Hardware.

Types of Multiprocessors. Shared Memory Symmetric Multiprocessors. Bus Architectures. LoadLocked/StoreConditional and Compare and Swap. Volatile: The Rest of the Story. Memory Systems. Reducing Cache Misses.



17. Examples.

Threads and Windows. Displaying Things for a Moment (Memory.java). Socket Server (Master/Slave Version). Socket Server (Producer/Consumer Version). Making a Native Call to pthread_setconcurrency( ). Actual Implementation of POSIX Synchronization. A Robust, Interruptible Server. Disk Performance with Java. Other Programs on the Web.



Appendix A: Internet.

Threads Newsgroup. Code Examples. Vendor's Threads Pages. Threads Research. Freeware Tools. Other Pointers. The Authors on the Net.



Appendix B: Books.

Threads Books. Related Books.



Appendix C: Timings.


Appendix D: APIs.

Function Descriptions. The Class java.lang.Thread. The Interface java.lang.Runnable. The Class java.lang.Object. The Class java.lang.ThreadLocal. The Class java.lang.ThreadGroup. Helper Classes from Our Extensions Library. The Class Extensions.InterruptibleThread. The Class Extensions.Semaphore. The Class Extensions.Mutex. The Class Extensions.ConditionVar. The Class Extensions.RWLock. The Class Extensions.Barrier. The Class Extensions.SingleBarrier. Glossary.



Index.

Preface

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.

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020