Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Concurrency in JDK 5.0

Brian Goetz (brian@quiotix.com), Principal Consultant, Quiotix

Brian Goetz is a regular columnist on the developerWorks Java zone and has been a professional software developer and manager for the past 18 years. He is a Principal Consultant at Quiotix, a software development and consulting firm in Los Altos, California.

Summary:  JDK 5.0 added major new support for developing concurrent applications, including JVM changes, new low-level synchronization utilities, and higher-level, thread-safe, high-performance concurrency classes such as thread pools, concurrent collections, semaphores, latches, and barriers. Learn how these new classes can help make your code faster, more scalable, more reliable, and easier to maintain.

Date:  23 Nov 2004
Level:  Introductory PDF:  A4 and Letter (189 KB | 32 pages)Get Adobe® Reader®

Activity:  67574 views
Comments:  

About this tutorial

What is this tutorial about?

JDK 5.0 is a major step forward for the creation of highly scalable concurrent applications in the Java language. The JVM has been improved to allow classes to take advantage of hardware-level concurrency support, and a rich set of new concurrency building blocks has been provided to make it easier to develop concurrent applications.

This tutorial covers the new utility classes for concurrency provided by JDK 5.0 and demonstrates how these classes offer improved scalability compared to the existing concurrency primitives (synchronized, wait(), and notify()).


Should I take this tutorial?

While this tutorial is aimed at a wide range of levels, it is assumed that readers have a basic understanding of threads, concurrency, and the concurrency primitives provided by the Java language, particularly the semantics and correct use of synchronization.

Beginning readers may wish to first consult the "Introduction to Java Threads" tutorial (see Resources), or read the concurrency chapter of a general purpose introductory text on the Java language.

Many of the classes in the java.util.concurrent package use generics, as java.util.concurrent has other strong dependencies on the JDK 5.0 JVM. Users not familiar with generics may wish to consult resources on the new generics facility in JDK 5.0. (For those not familiar with generics, you may find it useful to simply ignore whatever is inside the angle brackets in class and method signatures in your first pass through this tutorial.)


What's new in JDK 5.0 for concurrency

The java.util.concurrent package contains a wealth of thread-safe, well-tested, high-performance concurrent building blocks. The goal for the creation of java.util.concurrent was, quite immodestly, to do for concurrency what the Collections framework did for data structures. By providing a set of reliable, high-performance concurrency building blocks, developers can improve the thread safety, scalability, performance, readability, and reliability of their concurrent classes.

If some of the class names look familiar, it is probably because many of the concepts in java.util.concurrent are derived from Doug Lea's util.concurrent library (see Resources).

The improvements for concurrency in JDK 5.0 can be divided into three groups:

  • JVM-level changes. Most modern processors have some hardware-level support for concurrency, usually in the form of a compare-and-swap (CAS) instruction. CAS is a low-level, fine-grained technique for allowing multiple threads to update a single memory location while being able to detect and recover from interference from other threads. It is the basis for many high-performance concurrent algorithms. Prior to JDK 5.0, the only primitive in the Java language for coordinating access between threads was synchronization, which was more heavyweight and coarse-grained. Exposing CAS makes it possible to develop highly scalable concurrent Java classes. These changes are intended primarily for use by JDK library classes, not by developers.

  • Low-level utility classes -- locking and atomic variables. Using CAS as a concurrency primitive, the ReentrantLock class provides identical locking and memory semantics as the synchronized primitive, while offering better control over locking (such as timed lock waits, lock polling, and interruptible lock waits) and better scalability (higher performance under contention). Most developers will not use the ReentrantLock class directly, but instead will use the high-level classes built atop it.

  • High-level utility classes. These are classes that implement the concurrency building blocks described in every computer science text -- semaphores, mutexes, latches, barriers, exchangers, thread pools, and thread-safe collection classes. Most developers will be able to use these classes to replace many, if not all, uses of synchronization, wait(), and notify() in their applications, likely to the benefit of performance, readability, and correctness.

Roadmap

This tutorial will focus primarily on the higher-level utility classes provided by the java.util.concurrent package -- thread-safe collections, thread pools, and synchronization utilities. These are classes that both novices and experts can use "out of the box."

In the first section, we'll review the basics of concurrency, although it should not substitute for an understanding of threads and thread safety. Readers who are not familiar with threading at all should probably first consult an introduction to threads, such as the "Introduction to Java Threads" tutorial (see Resources).

The next several sections explore the high-level utility classes in java.util.concurrent -- thread-safe collections, thread pools, semaphores, and synchronizers.

The final sections cover the low-level concurrency building blocks in java.util.concurrent, and offer some performance measurements showing the improved scalability of the new java.util.concurrent classes.


Environmental requirements

The java.util.concurrent package is tightly tied to JDK 5.0; there is no backport to previous JVM versions. The code examples in this tutorial will not compile or run on JVMs prior to 5.0, and many of the code examples use generics, enhanced-for, or other new language features from JDK 5.0.

1 of 10 | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=131532
TutorialTitle=Concurrency in JDK 5.0
publish-date=11232004
author1-email=brian@quiotix.com
author1-email-cc=jaloi@us.ibm.com