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 ...
Get Java Threads, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.