站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Java Threads, Third Edition

Chapter 8. Threads and Collection Classes - Java Threads, Third Edition

Previous Section  < Day Day Up >  Next Section

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.

    Previous Section  < Day Day Up >  Next Section