What operations in Java are considered atomic?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the Java programming language, certain operations are guaranteed to be atomic. An atomic operation is one that is performed in a single step relative to other threads. It is indivisible, meaning that once an atomic operation is begun, it will complete without the possibility of interference from other threads.
Understanding Atomic Operations
Atomic operations in Java are fundamental in avoiding race conditions in concurrent programming. Here is a more detailed look at the types of operations considered atomic in Java:
Basic Atomic Operations
- Read and Write on `volatile` variables:
- A read or write operation on a `volatile` variable guarantees visibility, ensuring that changes to a variable are immediately visible to other threads. However, it does not guarantee atomicity beyond reads and writes.
- Example: When you assign a value to an `int` (32-bit) volatile variable or read from it, the operation is atomic.
- Reading and writing operations for `int`, `short`, `byte`, `char`, and `boolean` are atomic. This atomicity ensures that each write to such a variable is visible immediately to all other threads.
- Example:
- Reading from and assigning an object reference is atomic.
- Operations on `long` and `double`:
- 64-bit values are not guaranteed to be atomic unless declared with `volatile`. Operations on these types can suffer from tearing if not managed properly.
- Example:
- Increment (`++`) and Decrement (`--`):
- These are read-modify-write operations and thus are non-atomic because they involve multiple memory actions.
- Example:
- Assignment Operators with Arithmetic:
- Example includes `+=`, `-=`, etc. These operations first read, perform calculation, then write back, involving multiple steps.
- Example:
- Usage: These classes provide methods that ensure atomic updates without the use of synchronization methods like `synchronized` blocks.
- Example: Synchronizing access to a shared variable.
Related reading
- What q.defer really does?
- What resources are shared between threads?
- What server-side architectures could provide high availability and avoid race conditions?
- What should I decorate with asyncio.coroutine for async operations?
- What really happens in a try return x; finally x null; statement?
- What should I set JAVA_HOME environment variable on macOS X 10.6?
- What so different about Node.js's event-driven? Can't we do that in ASP.Net's HttpAsyncHandler?
- What will happen if shared memory application works on distributed memory architecture?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.