Ever since the first editions of Java Threads from O'Reilly and Concurrent Programming in Java from Addison-Wesley came out several years ago, I've been recommending them to people who want to learn to use threads. With several competing books, and second editions of the originals, now available, I have decided to reassess my recommendations. In this article, I'll look at six of the latest Java thread programming books.
The six books reviewed are:
- Java Threads, Second Edition, Scott Oaks and Henry Wong (O'Reilly)
- Concurrent Programming in Java, Second Edition, Doug Lea (Addison-Wesley)
- Taming Java Threads, Allen Holub (Apress)
- Java Thread Programming, Paul Hyde (Sams)
- Multithreaded Programming with Java Technology, Bil Lewis and Daniel Berg (Prentice Hall, Sun Press)
- High Performance Java Platform Computing: Multithreaded and Networked Programming, Thomas Christopher and George Thiruvathukal (Prentice Hall, Sun Press)
The table below provides a quick review of the six books' main characteristics.
|Threads Books Overview|
|Java Threads||Concurrent Programming||Taming Java Threads||Java Thread Programming||Multithreaded Programming||High Performance Java|
|CD-ROM/Source from Web||No / Yes||No / Yes||No / Yes||No / Yes||No / Yes||No / Yes|
|Techniques||Very Good||Good||Very Good||Excellent||Good||Very Good|
|Scale: None, Poor, Average, Good, Very Good, Excellent|
The table should be fairly self-explanatory up to the CD-ROM attribute. Let me further explain the last few rows:
- The Thread Basics attribute indicates the extent to which the books describe what a thread is and how to use it: describing the
notifyAll(), and the
- The Techniques attribute evaluates how well the books explore techniques in using threads.
- The Design Patterns attribute measures the books' explanation of how to create thread-based programming following well-defined design patterns.
Next, I will review each book. Beside each book's title is a star rating, based on the area of thread coverage on which that book claims to focus, not just an average of the three attributes listed above. One star indicates a poor job; the highest rating (five stars) designates exceptional coverage.
The first edition of Java Threads, by Scott Oaks and Henry Wong, was the original definitive resource for learning to program with threads. While most introductory books went over what a thread was and how to use the
Thread class with the
Runnable interface, Java Threads provided the additional information necessary to effectively use them. The second edition follows in its footsteps: it includes updated information on using threads with the Java 2 platform, information on the deprecated APIs, interrupting I/O, and the Java 2 security changes. The book also covers multithreading issues with the Swing component set, and provides help with native scheduling support and working with multiprocessor machines.
If you are new to Java programming and have yet to conquer threads, Java Threads is an excellent resource. You'll get a look into the basics of Java threading, and learn the techniques necessary to get your programs to function properly in a multithreaded world.
Concurrent Programming in Java
While Java Threads explains the low-level details of using the Threads API, Concurrent Programming in Java, by Doug Lea, explores proper design of multithreaded programs. Knowing how to use the API is not sufficient in the Java world; you should also spend the time and energy to get your program design right. In that light, Concurrent Programming is chiefly a design patterns book. Given a certain problem, you can look up the pattern defined in the book, and your program practically writes itself.
Concurrent Programming is broken into four areas that describe different usage patterns. The "Concurrent Object-Oriented Programming" section starts you with the basics. The "Exclusion" section describes patterns for using the
synchronized keyword. "State Dependence" explores issues with and patterns for using the
notifyAll() methods of the
Object class. The final section, "Creating Threads," is about patterns for using the
This book definitely caters to the advanced user, and is not for the faint of heart. You are expected to have a good grasp of object-oriented programming and the Java programming language, and only be lacking in the area of designing programs for concurrency issues. In addition to the book, Lea makes the util.concurrent library freely available at the book's Website. (See Resources for a link.) The library will help you design and develop multithreaded programs.
Taming Java Threads
Allen Holub's Taming Java Threads, originated here at JavaWorld as a nine-part Java Toolbox series. The articles have since been updated and now form the book's content, along with an interesting new chapter that describes the problems with Java's threading API. (See Resources for a link to that section, entitled "If I Were King.") Taming is unlike the first two books I reviewed; it is strictly about techniques for properly using the API. It is not an introduction to threads or a design patterns book. A code library (see Resources for a link) is also available so you can take advantage of what you've learned. Be sure to read the licensing agreement, though; it is not public domain or open source.
After describing the basic architecture of Java's multithreading model, Holub explains the perils of using threads and techniques for using them safely. You'll learn to create and use mutexes, semaphores, and conditional variables. You'll become familiar with the differences between Swing's
invokeAndWait() methods. Holub also explores the
AWTEventMulticaster class and shows how it is thread-safe with minimal overhead, and how you can work with thread pools. After all is said and done, you'll have a superb grasp on the limitations and usage of the Java threading model.
I was surprised to see some technical errors in the book -- in the preface, no less -- but it didn't detract from the excellent information the book offers. However, after continually seeing Java code formatted with mixed case, I was a little surprised that Holub's code was formatted with the underscore character between words in variables and method names. You may need to read the code blocks multiple times before you understand them fully, but the quality of information provided here is worth the inconvenience. One final note: all of the source code is fully javadoc-commented.
Java Thread Programming
Java Thread Programming, by Paul Hyde, directly competes with Java Threads. It clearly targets the novice developer, and includes additional coverage on more advanced topics and techniques for when that developer "grows up." This book is the largest of the bunch, and the space is put to good use.
The first half of Java Thread Programming explains what threads are and how to work with the basics. You get the typical description of creating threads and using the
Runnable interface. There is a chapter on how to stop threads, including how to suspend and resume them without using the deprecated methods -- nice techniques to increase productivity. In addition to covering
notify(), Hyde describes in detail the
volatile keyword and interthread communication through I/O pipes. Java Thread Programming also has the best coverage of the
InheritableThreadLocal classes, in addition to the usual coverage of threads and Swing found in other books.
Part 2 of Java Thread Programming describes techniques for working with Java threads. It offers the best thread-pooling coverage and a great chapter on breaking out of blocked I/O. You won't be disappointed with this selection.
Multithreaded Programming with Java Technology
I seem to always find at least one book that's not quite like the others -- Multithreaded Programming with Java Technology, by Bil Lewis and Daniel Berg, is just such a beast. I still gave it a star rating, but this book deserves special mention. In my opinion, Multithreaded Programming's primary purpose is not to teach you to program with threads in Java. It will help you in that area, but its primary purpose seems to be to teach you how the underlying thread architecture works, both within the Java Virtual Machine (Java VM) and the underlying operating systems. In fact, you not only get many of the book's code examples in Java code, you also get POSIX code and code for the Win32 API thread handling. If there were reader exercises at the end of each chapter, this would work well as a college text.
That said, how well does Multithreaded Programming teach you how to use threads? It most closely resembles Java Threads; its main goal is to teach you what a thread is and how to use it. It also shows how everything under the hood works in the Java VM, how everything is scheduled within the Java VM, and so on. In addition, you get coverage of typical threading topics like synchronization, deadlocks, and race conditions. Lewis and Berg even cover thread issues with RMI and optimization techniques to improve performance. All in all, this is a good book -- just not your typical presentation of Java threads.
As far as the book's examples go, watch out for Elvis getting caught by the UFOs.
High Performance Java Platform Computing
Checking in with the longest title is High Performance Java Platform Computing: Multithreaded and Networked Programming, by Thomas Christopher and George Thiruvathukal. Even the authors' last names are at least double the length of their competition's! The excess doesn't stop there; this book also costs over 25 percent more than the others.
Enough about the outside -- what's inside? High Performance jumps right into a discussion of parallel computing. You'll learn about the von Neumann Machine, Flynn's Taxonomy, linear speedup, and Amdahl's Law, all in Chapter 1. Chapter 2 explores thread creation in more depth than most introductions to the API. After a demonstration of race conditions, Chapter 3 discusses synchronization and why you need it. Chapter 4 delves deeply into monitors, with many variations on reader-writer lock problems. In Chapter 5, the authors discuss the use of shared memory -- on the heap, not the Unix system variety -- all for the purpose of having a thread pool. The rest of the book covers patterns for developing applications for parallel execution.
There is lots of good information here on parallel computing, but High Performance's goal is not really to teach general-purpose thread programming. It seems to be developed mainly as a text for a parallel computing course -- each chapter ends with a series of thoughtful exercises, to which I failed to find any answers.
Do I keep recommending the same?
The first editions of Concurrent Programming in Java and Java Threads came out in 1997 and were highly recommended. Am I just blindly recommending their second editions? In short, it seems that Lea's Concurrent Programming is still an excellent resource for those learning to design programs for the multithreaded world. It's not an easy read, but the design patterns and techniques explained in the book are excellent tools for serious Java developers.
On the other hand, it appears Java Threads has been succeeded by Sams's Java Thread Programming as the best book for beginners. Nothing is technically wrong with the O'Reilly offering, but Java Thread Programming works better as an introduction to Java threads. Hyde even goes beyond the introductory material, offering more advanced techniques for an extra .
If you have the first editions of Java Threads and Concurrent Programming in Java, are the updates worth getting? I might recommend getting Lea's second edition and passing on Java Threads. If you have the first edition of Java Threads, you have probably moved beyond the need for a second one by now. If you still need an introductory text, pick up Sams's Java Thread Programming instead.
What to do with the rest? It's really a tough call and depends whose dime you're spending. If I could only get one more threads book, I would choose Multithreaded Programming with Java Technology. Its thread coverage is unique and useful enough to make it a nice addition to a library. Holub's Taming Java Threads contains some nice techniques for getting the most out of thread programming. I'm not sure if it's a necessary resource, though. High Performance Java Platform Computing seems appropriate for those interested in the parallel computing world. Otherwise, I'd leave it on the shelf.