Skip to Main Content
Java Threads, 3rd Edition
book

Java Threads, 3rd Edition

by Scott Oaks, Henry Wong
September 2004
Intermediate to advanced content levelIntermediate to advanced
362 pages
11h 24m
English
O'Reilly Media, Inc.
Content preview from Java Threads, 3rd Edition

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Performance: The Definitive Guide

Java Performance: The Definitive Guide

Scott Oaks
Java Concurrency, 2/e

Java Concurrency, 2/e

Douglas Schmidt
Java Swing, 2nd Edition

Java Swing, 2nd Edition

Dave Wood, Robert Eckstein, Marc Loy, James Elliott, Brian Cole

Publisher Resources

ISBN: 0596007825Supplemental ContentErrata Page