java search
Loading
Help improve Java Search

Thursday, November 16, 2006

Java 5: New features in Concurrency

Most of the new features in concurrency are implemented in the java.util.concurrent packages. There are also new concurrent data structures in the Java Collections Framework:
  • Lock objects support locking idioms that simplify many concurrent applications.
  • Executors define a high-level API for launching and managing threads. Executor implementations provided by java.util.concurrent provide thread pool management suitable for large-scale applications.
  • Concurrent collections make it easier to manage large collections of data, and can greatly reduce the need for synchronization.
  • Atomic variables have features that minimize synchronization and help avoid memory consistency errors.
LOCK OBJECTS
Lock objects work very much like the implicit locks (monitors) used by synchronized code. As with implicit locks, only one thread can own a Lock object at a time. Lock objects also support a wait/notify mechanism, through their associated Condition objects. All the lock objects are defined in the java.util.concurrent.lock package. The biggest advantage of Lock objects over implicit locks is their ability to back out of an attempt to acquire a lock. The tryLock method backs out if the lock is not available immediately or before a timeout expires (if specified). The lockInterruptibly method backs out if another thread sends an interrupt before the lock is acquired.

EXECUTORS
The java.util.concurrent package defines three executor interfaces:
  • Executor: A simple interface that supports launching new tasks.
  • ExecutorService: A sub-interface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
  • ScheduledExecutorService: A sub-interface of ExecutorService, supports future and/or periodic execution of tasks.
CONCURRENT COLLECTIONS
The java.util.concurrent package includes a number of additions to the Java Collections Framework.
  • BlockingQueue defines a first-in-first-out data structure that blocks or times out when you attempt to add to a full queue, or retrieve from an empty queue.
  • ConcurrentMap is a subinterface of java.util.Map that defines useful atomic operations. These operations remove or replace a key-value pair only if the key is present, or add a key-value pair only if the key is absent. The standard general-purpose implementation of ConcurrentMap is ConcurrentHashMap, which is a concurrent analog of HashMap.
  • ConcurrentNavigableMap is a subinterface of ConcurrentMap that supports approximate matches. The standard general-purpose implementation of ConcurrentNavigableMap is ConcurrentSkipListMap, which is a concurrent analog of TreeMap.
ATOMIC VARIABLES
The java.util.concurrent.atomic package defines classes that support atomic operations on single variables (ex. AtomicInteger). All classes have get and set methods that work like reads and writes on volatile variables. The atomic compareAndSet method also has these memory consistency features, as do the simple atomic arithmetic methods that apply to integer atomic variables.

References

No comments:

Post a Comment