Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explain atomic actions?

What are atomic actions and why they are neccessary? Also, How are atomic actions implemented in Java?

My understanding is that in programming an atomic action is one that effectively happens all at one. An atomic action cannot stop in the middle it either happens completely or not at all.

For example, ordering an airline ticket online where two actions are required: payment and a seat reservation. The potential passenger must either.

  1. both pay and reserve a seat, OR
  2. neither pay nor reserve a seat
like image 809
user1014888 Avatar asked Jan 07 '12 18:01

user1014888


People also ask

What is the meaning of atomic action?

An indivisible sequence of primitive operations that must complete without interruption (or that can be expected to do so), or can be considered as instantaneous.

What does atomic execution mean?

The atomic execution of a process means that it enjoys the following two properties: All-or-nothing: the concrete process is either executed completely, or not at all; this implies that the process is not observable during its execution, but only before and after.

How do atomic functions work?

During an atomic operation, a processor can read and write a location during the same data transmission. In this way, another input/output mechanism or processor cannot perform memory reading or writing tasks until the atomic operation has finished.

What is an atomic operation example?

An example of atomic operation is instruction execution, usually an instruction feed to the execution unit can't be stopped in the middle. Yet, a statement in high level language results in multiple instructions. It is the root cause of non-atomic operations.


2 Answers

Your explanation, IMHO, rather explains what atomicity means regarding database transactions: the A in ACID.

Regarding concurrency, atomicity rather means that when a thread modifies the state of some object (or set of objects), another thread can't see any intermediary state. Either it sees the state as it was before the operation, or it sees the state as it is after the operation.

For example, changing the value of a long variable is not an atomic operation. It involves setting the value of the 32 first bits, and then setting the state of the 32 last bits. If the access to the long variable is not properly synchronized, a thread might see the intermediary state: the 32 first bits have been changed, but the 32 last bits haven't been changed yet.

The way to implement atomic operations is to use synchronization. synchronization involves using

  • the synchronized keyword
  • the volatile keyword
  • atomic variables (AtomicInteger, etc.)
like image 79
JB Nizet Avatar answered Nov 08 '22 02:11

JB Nizet


Maybe you should think about transactions. Make some process but do not save changes until everything is in order. Like when you are withdrawing money from a machine, you follow a series of steps, before seeing changes on your account, i.e. Put your card, your password, say how much money you want, receive the money. If something fails in one of the steps, you dont see changes in your savings account, e.g. your password was incorrect, or you are trying to withdraw more money than what you have...

You can read the java tutorial. http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

like image 38
Roger Avatar answered Nov 08 '22 00:11

Roger