Java Concurrency
- Concurrency Programming in Java
- Concurrency through java Synchronization
- High Level Concurrency Objects
Concurrency Programming in Java |
- How to write applications that perform multiple tasks simultaneously.
- The Java platform is designed from the ground up to support concurrent programming, with basic concurrency support in the Java programming language and the Java class libraries.
- Since version 5.0, the Java platform has also included high-level concurrency APIs.
- The high-level APIs are available in the java.util.concurrent packages.
Concurrency through java Synchronization |
Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors.
The tool needed to prevent these errors is synchronization.
- Thread Interference describes how errors are introduced when multiple threads access shared data.
- Memory Consistency Errors describes errors that result from inconsistent views of shared memory.
- Synchronized Methods describes a simple idiom that can effectively prevent thread interference and memory consistency errors.
- Implicit Locks and Synchronization – general synchronization idiom, and describes how synchronization is based on implicit locks.
- Atomic Access talks about the general idea of operations that can’t be interfered with by other threads.
High Level Concurrency Objects |
These APIs are adequate for very basic tasks, but higher-level building blocks are needed for more advanced tasks. This is especially true for massively concurrent applications that fully exploit today’s multiprocessor and multi-core systems.
Some of the high-level concurrency features introduced with version 5.0 of the Java platform. Most of these features are implemented in the new 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.
Atomic Variables without Concurrency package
class MyCounter {
private int ctr = 0;
public void increment() {
ctr++;
}
public void decrement() {
ctr--;
}
public int value() {
return ctr;
}
}
<b>Here is the way to make MyCounter safe from thread interference
by making methods synchronized.</b>
class MySynchronizedCounter {
private int ctr = 0;
public synchronized void increment() {
ctr++;
}
public synchronized void decrement() {
ctr--;
}
public synchronized int value() {
return ctr;
}
}
java.util.concurrent.atomic.AtomicInteger
For this simple class, synchronization is an acceptable solution. But for a more complicated class, we might want to avoid the liveness impact of unnecessary synchronization. Replacing the int field with an AtomicInteger allows us to prevent thread interference without resorting to synchronization and it is as below.
import java.util.concurrent.atomic.AtomicInteger;
class AtomicCounter {
private AtomicInteger ctr = new AtomicInteger(0);
public void increment() {
ctr.incrementAndGet();
}
public void decrement() {
ctr.decrementAndGet();
}
public int value() {
return ctr.get();
}
}
Recent Comments