background preloader

Concurrency

Facebook Twitter

More flexible, scalable locking in JDK 5.0. Multithreading and concurrency are nothing new, but one of the innovations of the Java language design was that it was the first mainstream programming language to incorporate a cross-platform threading model and formal memory model directly into the language specification.

More flexible, scalable locking in JDK 5.0

The core class libraries include a Thread class for creating, starting, and manipulating threads, and the language includes constructs for communicating concurrency constraints across threads -- synchronized and volatile. While this simplifies the development of platform-independent concurrent classes, it by no means makes writing concurrent classes trivial -- just easier. A quick review of synchronized Declaring a block of code to be synchronized has two important consequences, generally referred to as atomicity and visibility.

Synchronized (lockObject) { // update object state } Java theory and practice: Going atomic. Fifteen years ago, multiprocessor systems were highly specialized systems costing hundreds of thousands of dollars (and most of them had two to four processors).

Java theory and practice: Going atomic

Today, multiprocessor systems are cheap and plentiful, nearly every major microprocessor has built-in support for multiprocessing, and many support dozens or hundreds of processors. To exploit the power of multiprocessor systems, applications are generally structured using multiple threads. Introduction to nonblocking algorithms. When more than one thread accesses a mutable variable, all threads must use synchronization, or else some very bad things can happen.

Introduction to nonblocking algorithms

The primary means of synchronization in the Java language is the synchronized keyword (also known as intrinsic locking), which enforces mutual exclusion and ensures that the actions of one thread executing a synchronized block are visible to other threads that later execute a synchronized block protected by the same lock. When used properly, intrinsic locking can make our programs thread-safe, but locking can be a relatively heavyweight operation when used to protect short code paths when threads frequently contend for the lock. In "Going atomic,"we looked at atomic variables, which provide atomic read-modify-write operations for safely updating shared variables without locks.

A nonblocking counter.