O'Reilly logo

Java Threads, 3rd Edition by Henry Wong, Scott Oaks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Threads and Collection Classes

In this chapter, we'll look at how threads interact with the collection classes provided by Java. We'll examine some synchronization issues and how they affect our choice and usage of collection classes.

The collection classes comprise many of the classes in the java.util package (and, in J2SE 5.0, some of the classes in the java.util.concurrent package). Collection classes are used to store objects in some data structure: a hashtable, an array, a queue, and so on. Collection classes interact with Java threads in a few areas:

  • Collection classes may or may not be threadsafe, so threads that use those classes must understand their synchronization requirements.

  • Not all collections have the same performance with regard to thread synchronization, so threads that use them must understand the conditions in which they can be used optimally.

  • Newer collection classes automatically provide some threading semantics (such as using thread notification when their data changes).

  • Threads commonly use collection classes to share data.

We begin this chapter with an overview of the collection classes; the overview addresses the thread-safety of the various classes. Next, we show how some of the newer collection classes interact with threads. And finally, we show a common design pattern in which multiple threads use the collections: the producer-consumer model.

Overview of Collection Classes

In the beginning, Java provided only a few collection classes. In fact, in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required