Java Threads, Second Edition


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 ...

See more details below
Available through our Marketplace sellers.
Other sellers (Paperback)
  • All (29) from $1.99   
  • New (7) from $4.22   
  • Used (22) from $1.99   
Sort by
Page 1 of 1
Showing All
Note: Marketplace items are not eligible for any coupons and promotions
Seller since 2015

Feedback rating:



New — never opened or used in original packaging.

Like New — packaging may have been opened. A "Like New" item is suitable to give as a gift.

Very Good — may have minor signs of wear on packaging but item works perfectly and has no damage.

Good — item is in good condition but packaging may have signs of shelf wear/aging or torn packaging. All specific defects should be noted in the Comments section associated with each item.

Acceptable — item is in working order but may show signs of wear such as scratches or torn packaging. All specific defects should be noted in the Comments section associated with each item.

Used — An item that has been opened and may show signs of wear. All specific defects should be noted in the Comments section associated with each item.

Refurbished — A used item that has been renewed or updated and verified to be in proper working condition. Not necessarily completed by the original manufacturer.

1999 Trade paperback 2nd ed. New. Trade paperback (US). Glued binding. 332 p. Contains: Illustrations. Java (O'Reilly).

Ships from: San Mateo, CA

Usually ships in 1-2 business days

  • Canadian
  • International
  • Standard, 48 States
  • Standard (AK, HI)
  • Express, 48 States
  • Express (AK, HI)
Seller since 2010

Feedback rating:


Condition: New
PAPERBACK New 1565924185 NEW, UNOPENED, NEAR FLAWLESS EVERYTHING. RIGHT OFF THE BOOKSTORE SHELF. NO NAMES OR MARKS SEEN. NOT EX-LIB OR A REMAINDER. Village Idiot's Books has ... overpacking with cardboard wrapped around most books to protect, and prompt shipping in waterproof bubble-wrap bag. Legendary Service and NO HASSLE Policy. 03 05 2011 TB-953 3.. Read more Show Less

Ships from: Durham, NH

Usually ships in 1-2 business days

  • Canadian
  • International
  • Standard, 48 States
  • Standard (AK, HI)
  • Express, 48 States
  • Express (AK, HI)
Seller since 2007

Feedback rating:


Condition: New
1999-01-27 Paperback New This book, SHIPS TO USA ONLY and MEDIA/STANDARD mail ONLY! ! Paperback. (Different cover art from picture shown). You are buying a Book in NEW ... condition with very light shelf wear. Read more Show Less

Ships from: Wilmington, NC

Usually ships in 1-2 business days

  • Canadian
  • Standard, 48 States
  • Standard (AK, HI)
  • Express, 48 States
  • Express (AK, HI)
Seller since 2006

Feedback rating:


Condition: New
1999 Trade paperback 2nd ed. Illustrated. New. No dust jacket as issued. Book is in new condition. Pages are bright and unmarked. Trade paperback (US). Glued binding. 319 p. ... Contains: Illustrations. Java Series. Audience: General/trade. Read more Show Less

Ships from: Pasadena, CA

Usually ships in 1-2 business days

  • Canadian
  • International
  • Standard, 48 States
  • Standard (AK, HI)
  • Express, 48 States
  • Express (AK, HI)
Seller since 2015

Feedback rating:


Condition: New
Brand new.

Ships from: acton, MA

Usually ships in 1-2 business days

  • Standard, 48 States
  • Standard (AK, HI)
Seller since 2015

Feedback rating:


Condition: New
Brand new.

Ships from: acton, MA

Usually ships in 1-2 business days

  • Standard, 48 States
  • Standard (AK, HI)
Seller since 2008

Feedback rating:


Condition: New

Ships from: Chicago, IL

Usually ships in 1-2 business days

  • Standard, 48 States
  • Standard (AK, HI)
Page 1 of 1
Showing All
Sort by
Sending request ...


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 Threads discusses 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 Threads shows 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 Threads covers 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.

Read More Show Less

Editorial Reviews

Library Journal
Cadenhead's Teach Yourself Java 2 in 24 Hours is a definite beginner's book, a self-tutorial in 24 one-hour chapters. The guides on networking, Threads (the ability of Java to multitask by allowing for multiple processes and actions at the same time), and Swing (a powerful Java interface package for visual design) are for advanced Java programmers but will fit well within most libraries. Copyright 1999 Cahners Business Information.
Library Journal
O'Reilly books have a reputation among programmers for providing some of the best technical information for professionals. No exception, these three web-related books will only enhance O'Reilly's reputation. JavaScript is not Java, but it is very useful because JavaScript code does not need to be compiled and the scripts can be embedded directly into an HTML document. Flanagan's work is an excellent book for programmers interested in learning it quickly. Grand, meanwhile, provides an exceptionally clear discussion of Java itself that is particularly useful for a working programmer moving from C++ to Java. Threads are what makes Java a particularly useful language for multiprocessingthe ability to appear to do more than one thing at a timewhich is what the Internet is all about. The tricky part of threads is that the concept is new for most users. Oaks offers a very clear discussion of how to spawn a process, when to spawn, and how to synchronize and schedule it, all illustrated with good network examples.
Read More Show Less

Product Details

  • ISBN-13: 9781565924185
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 1/27/1999
  • Series: Java Series
  • Edition description: Second Edition
  • Edition number: 2
  • Pages: 344
  • Product dimensions: 6.99 (w) x 9.12 (h) x 0.72 (d)

Meet the Author

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.

Read More Show Less

Read an Excerpt

Chapter 1: Introduction to Threading

...Why Threads?

The notion of threading is so ingrained in Java that it's almost impossible to write even the simplest programs in Java without creating and using threads. And many of the classes in the Java API are already threaded, so that often you are using multiple threads without realizing it.

Historically, threading was first exploited to make certain programs easier to write: if a program can be split into separate tasks, it's often easier to program the algorithm as separate tasks or threads. Programs that fall into this category are typically specialized and deal with multiple independent tasks. The relative rareness of these types of programs makes threading in this category a specialized skill. Often, these programs were written as separate processes using operating-system-dependent communication tools such as signals and shared memory spaces to communicate between processes. This approach increased system complexity.

The popularity of threading increased when graphical inter-faces became the standard for desktop computers because the threading system allowed the user to perceive better program performance. The introduction of threads into these platforms didn't make the programs any faster, but it did create an illusion of faster performance for the user, who now had a dedicated thread to service input or display output.

Recently, there's been a flurry of activity regarding a new use of threaded programs: to exploit the growing number of computers that have multiple processors. Programs that require a lot of CPU processing are natural candidates for this category, since a calculation that requires one hour on a single-processor machine could (at least theoretically) run in half an hour on a two-processor machine, or 15 minutes on a four-processor machine. All that is required is that the program be written to use multiple threads to perform the calculation.

While computers with multiple processors have been around for a long time, we're now seeing these machines become cheap enough to be very widely available. The advent of less expensive machines with multiple processors, and of operating systems that provide programmers with thread libraries to exploit those processors, has made threaded programming a hot topic, as developers move to extract every benefit from these new machines. Until Java, much of the interest in threading centered around using threads to take advantage of multiple processors on a single machine.

However, threading in Java often has nothing at all to do with multiprocessor machines and their capabilities; in fact, the first Java virtual machines were unable to take advantage of multiple processors on a machine, and many implementations of the virtual machine still follow that model. However, there are also implementations of the virtual machine that do take advantage of the multiple processors that the computer may have. A correctly written program running in one of those virtual machines on a computer with two processors may indeed take roughly half the time to execute that it would take on a computer with a single processor. If you're looking to use Java to have your program scale to many processors, that is indeed possible when you use the correct virtual machine. However, even if your Java program is destined to be run on a machine with a single CPU, threading is still very important.

The major reason threading is so important in Java is that Java has no concept of asynchronous behavior. This means that many of the programming techniques you've become accustomed to using in typical programs are not applicable in Java; instead, you must learn a new repertoire of threading techniques to handle these cases of asynchronous behavior.

This is not to say there aren't other times when threads are a handy programming technique in Java; certainly it's easy to use Java for a program that implements an algorithm that naturally lends itself to threading. And many Java programs implement multiple independent behaviors. The next few sections cover some of the circumstances in which Java threads are a required component of the program, due to the need for asynchronous behavior or to the elegance that threading lends to the problem.

Nonblocking I/O

In Java, as in most programming languages, when you try to get input from the user, you execute a read () method specifying the user's terminal ( in Java). When the program executes the read () method, the program will typically wait until the user types at least one character before it continues and executes the next statement. This type of I/O is called blocking I/O: the program blocks until some data is available to satisfy the read () method.

This type of behavior is often undesirable. If you're reading data from a network socket, that data is often not available when you want to read it: the data may have been delayed in transit over the network, or you may be reading from a network server that sends data only periodically. If the program blocks when it tries to read from the socket, then it's unable to do anything else until the data is actually available. If the program has a user interface that contains a button and the user presses the button while the program is executing the read () method, nothing will happen: the program will be unable to process the mouse events and execute the event-processing method associated with the button. This can be very frustrating for the user, who thinks the program has hung.

Traditionally, there are three techniques to cope with this situation:

I/O multiplexing

Developers often take all input sources and use a system call like select () to notify them when data is available from a particular source. This allows input to be handled much like an event from the user (in fact, many graphical tool kits use this method transparently to the user, who simply registers a callback function that is called whenever data is available from a particular source).


Polling allows a developer to test if data is available from a particular source. If data is available, the data can be read and processed; if it is not, the program can perform another task. Polling can be done either explicitly--with a system call like poll ()--or, in some systems, by making the read () function return an indication that no data is immediately available.


A file descriptor representing an input source can often be set so that an asynchronous signal is delivered to the program when data is available on that input source. This signal interrupts the program, which processes the data and then returns to whatever task it had been doing.

In Java, none of these techniques is directly available. There is limited support for polling via the available () method of the FilterInputStream class, but this method does not have the rich semantics that polling typically has in most operating systems. To compensate for the lack of these features, a Java developer must set up a separate thread to read the data. This separate thread can block when data isn't available, and the other thread (s) in the Java program can process events from the user or perform other tasks.

While this issue of blocking I/O can conceivably occur with any data source, it occurs most frequently with network sockets. If you're used to programming sockets, you've probably used one of these techniques to read from a socket, but perhaps not to write to one. Many developers, used to programming on a local area network, are vaguely aware that writing to a socket may block, but it's a possibility that many of them ignore because it can only happen under certain circumstances, such as a backlog in getting data onto the network. This backlog rarely happens on a fast local area network, but if you're using Java to program sockets over the Internet, the chances of this backlog happening are greatly increased; hence the chance of blocking while attempting to write data onto the network is also increased. So in Java, you may need two threads to handle the socket: one to read from the socket and one to write to it...

Read More Show Less

Table of Contents

Who Should Read This Book?;
Versions Used in This Book;
Organization of This Book;
Conventions Used in This Book;
Feedback for Authors;
Chapter 1: Introduction to Threading;
1.1 Java Terms;
1.2 Thread Overview;
1.3 Why Threads?;
1.4 Summary;
Chapter 2: The Java ThreadingAPI;
2.1 Threading Using the Thread Class;
2.2 Threading Using the Runnable Interface;
2.3 The Life Cycle of a Thread;
2.4 Thread Naming;
2.5 Thread Access;
2.6 More on Starting, Stopping, and Joining;
2.7 Summary;
Chapter 3: Synchronization Techniques;
3.1 A Banking Example;
3.2 Reading Data Asynchronously;
3.3 A Class to Perform Synchronization;
3.4 The Synchronized Block;
3.5 Nested Locks;
3.6 Deadlock;
3.7 Return to the Banking Example;
3.8 Synchronizing Static Methods;
3.9 Summary;
Chapter 4: Wait and Notify;
4.1 Back to Work (at the Bank);
4.2 Wait and Notify;
4.3 wait(), notify(), and notifyAll();
4.4 wait() and sleep();
4.5 Thread Interruption;
4.6 Static Methods (Synchronization Details);
4.7 Summary;
Chapter 5: Useful Examplesof Java Thread Programming;
5.1 Data Structures and Containers;
5.2 Simple Synchronization Examples;
5.3 A Network Server Class;
5.4 The AsyncInputStream Class;
5.5 Using TCPServer with AsyncInputStreams;
5.6 Summary;
Chapter 6: Java Thread Scheduling;
6.1 An Overview of Thread Scheduling;
6.2 When Scheduling Is Important;
6.3 Scheduling with Thread Priorities;
6.4 Popular Scheduling Implementations;
6.5 Native Scheduling Support;
6.6 Other Thread-Scheduling Methods;
6.7 Summary;
Chapter 7: Java Thread Scheduling Examples;
7.1 Thread Pools;
7.2 Round-Robin Scheduling;
7.3 Job Scheduling;
7.4 Summary;
Chapter 8: Advanced Synchronization Topics;
8.1 Synchronization Terms;
8.2 Preventing Deadlock;
8.3 Lock Starvation;
8.4 Thread-Unsafe Classes;
8.5 Summary;
Chapter 9: Parallelizing for Multiprocessor Machines;
9.1 Parallelizing a Single-Threaded Program;
9.2 Inner-Loop Threading;
9.3 Loop Printing;
9.4 Multiprocessor Scaling;
9.5 Summary;
Chapter 10: Thread Groups;
10.1 Thread Group Concepts;
10.2 Creating Thread Groups;
10.3 Thread Group Methods;
10.4 Manipulating Thread Groups;
10.5 Thread Groups, Threads, and Security;
10.6 Summary;
Miscellaneous Topics;
Thread Stack Information;
General Thread Information;
Default Exception Handler;
The ThreadDeath Class;
The Volatile Keyword;
Exceptions and Errors;

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)