Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "atomic" mean in programming?

Tags:

java

atomic

In the Effective Java book, it states:

The language specification guarantees that reading or writing a variable is atomic unless the variable is of type long or double [JLS, 17.4.7].

What does "atomic" mean in the context of Java programming, or programming in general?

like image 781
James Avatar asked Feb 24 '13 16:02

James


People also ask

What are atomic variables in coding?

You can think of these are wrapper of primitive types boolean, integer and long, with the difference: they are designed to be safely used in multi-threaded context. They are called atomic variables because they provide some operations that cannot be interfered by multiple threads.

What does atomic mean in database?

Atomicity means that multiple operations can be grouped into a single logical entity, that is, other threads of control accessing the database will either see all of the changes or none of the changes.

What does atomic mean in C++?

Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.

What does atomic mean in Java?

Atomic is a toolkit of variable java. util. concurrent. atomic package classes, which assist in writing lock and wait-free algorithms with the Java language. An algorithm requiring only partial threads for constant progress is lock-free.


2 Answers

Here's an example: Suppose foo is a variable of type long, then the following operation is not an atomic operation (in Java):

foo = 65465498L; 

Indeed, the variable is written using two separate operations: one that writes the first 32 bits, and a second one which writes the last 32 bits. That means that another thread might read the value of foo, and see the intermediate state.

Making the operation atomic consists in using synchronization mechanisms in order to make sure that the operation is seen, from any other thread, as a single, atomic (i.e. not splittable in parts), operation. That means that any other thread, once the operation is made atomic, will either see the value of foo before the assignment, or after the assignment. But never the intermediate value.

A simple way of doing this is to make the variable volatile:

private volatile long foo; 

Or to synchronize every access to the variable:

public synchronized void setFoo(long value) {     this.foo = value; }  public synchronized long getFoo() {     return this.foo; } // no other use of foo outside of these two methods, unless also synchronized 

Or to replace it with an AtomicLong:

private AtomicLong foo; 
like image 55
JB Nizet Avatar answered Sep 19 '22 13:09

JB Nizet


"Atomic operation" means an operation that appears to be instantaneous from the perspective of all other threads. You don't need to worry about a partly complete operation when the guarantee applies.

like image 45
H2ONaCl Avatar answered Sep 21 '22 13:09

H2ONaCl