- Java Atomic Operations And Nonblocking Algorithms Tutorial With Examples
- Atomic Variables
- Nonblocking algorithms
- java.util.concurrent.atomic package
- What is atomic and non atomic operations in Java?
- What is atomic data type in Java?
- What is difference between atomic and volatile in Java?
- What is atomic number in Java?
- What is deadlock Java?
- How can we prevent deadlock in spring?
Java Atomic Operations And Nonblocking Algorithms Tutorial With Examples
An operation during which a processor can simultaneously read a location and write it in the same bus operation. This prevents any other processor or I/O device from writing or reading memory until the operation is complete.
In Java, the reading and writing of 32-bit or smaller quantities are guaranteed to be atomic. By atomic we mean each action takes place in one step and cannot be interrupted.
For example, the following code is thread safe:
However, we should be careful to note that the guarentee applies only to reading and writing. For eg. check the following code snippet:
- Read current setting of ‘value’.
- Increment the setting.
- Write the new setting back.
- Threading bugs are difficult to detect and are time consuming.
- This code snippet might translate into a single instruction on some CPUs and thus work correctly. The problem might arise when tested with other JVMs.
Atomic Variables
The java.util.concurrent.atomic package defines classes that support atomic operations on single variables. All classes have get and set methods that work like reads and writes on volatile variables. That is, a set has a happens-before relationship with any subsequent get on the same variable. The atomic compareAndSet method also has these memory consistency features, as do the simple atomic arithmetic methods that apply to integer atomic variables. To see how this package might be used, let’s return to the Counter class:
public class Counter < private int x = 0; public void increment() < ++this.value; >public void decrement() < --this.value; >public void value() < return x; >>
One way to make Counter safe from thread interference is to make its methods synchronized, as in SynchronizedCounter:
public class SynchronizedCounter < private int x = 0; public synchronized void increment() < ++this.value; >public synchronized void decrement() < --this.value; >public synchronized int value() < return x; >>
For this simple class, synchronization is an acceptable solution. Now Counter is thread-safe, but the need to use a lock irks some developers because of the performance cost involved.
Nonblocking algorithms
Java 5.0 provides supports for additional atomic operations. This allows to develop algorithm which are non-blocking algorithm, e.g. which do not require synchronization, but are based on low-level atomic hardware primitives such as compare-and-swap (CAS). A compare-and-swap operation check if the variable has a certain value and if it has this value it will perform this operation.
Non-blocking algorithm are usually much faster then blocking algorithms as the synchronization of threads appears on a much finer level (hardware).
import java.util.concurrent.atomic.AtomicInteger; class AtomicCounter < private AtomicInteger c = new AtomicInteger(0); public void increment() < c.incrementAndGet(); >public void decrement() < c.decrementAndGet(); >public int value() < return c.get(); >>
java.util.concurrent.atomic package
- AtomicBoolean
- AtomicInteger
- AtomicIntegerArray
- AtomicIntegerFieldUpdater
- AtomicLong
- AtomicLongArray
- AtomicLongFieldUpdater
- AtomicReference
Hope we are able to explain you java Atomic Operations, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).
Please share us on social media if you like the tutorial.
What is atomic and non atomic operations in Java?
Atomic operations are take place in one step. Like read and write operation of variable. atomic operations cannot be interrupted and they are thread safe, In java read and write operations are atomic for all variables which are less or equal to 32 bit.
What is atomic data type in Java?
The most commonly used atomic variable classes in Java are AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference. These classes represent an int, long, boolean, and object reference respectively which can be atomically updated.
Is assignment in Java Atomic?
Java Specification says that assignment to variables smaller than or equal to 32 bits is an atomic operation, which excludes variables of types double and long (both are 64 bits).
What is difference between atomic and volatile in Java?
Volatile and Atomic are two different concepts. Volatile ensures, that a certain, expected (memory) state is true across different threads, while Atomics ensure that operation on variables are performed atomically.
What is atomic number in Java?
AtomicInteger class provides operations on underlying int value that can be read and written atomically, and also contains advanced atomic operations. AtomicInteger supports atomic operations on underlying int variable. It have get and set methods that work like reads and writes on volatile variables.
Which method releases the lock in Java?
Thread inside the synchronized method is set as the owner of the lock and is in RUNNABLE state. Any thread that attempts to enter the locked method becomes BLOCKED. When thread calls wait it releases the current object lock (it keeps all locks from other objects) and than goes to WAITING state.
What is deadlock Java?
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object.
How can we prevent deadlock in spring?
- Try to avoid nested synchronization blocks.
- Lock ordering: If you can’t avoid nested synchronization block then you should make sure that threads will acquire locks in the same order.
- Lock Timeout: You can also specify a timeout.