Atomicity with Java
What is Atomicity
Atomicity, in computer science, is considered to be a property [ALL-OR-NOTHING], that the state of a resource in an operation is not controlled by a different flow of execution at the same time [Even in time slicing operations]
We know, Java is not pure object oriented programming language, due to primitive data types' design behavior.
Most of the Java experts, while designing and developing applications thrives hard, to create, most secured applications considering "Atomicity in multi threaded application".
Does Java language helps in developing Secured application..?
The answer is no, Java programming language does not give secured applications at all, especially in multi threaded environment.
As we know, Java supports 2 types of variables/data types.
- Primitive data types
- User defined data types
Primitive data types:
Java supports 8 different primitive data types. They are:
- boolean 1 byte
- short 2 bytes
- int 4 bytes
- char 2 bytes
- float 4 bytes
- long 8 bytes
- double 8 bytes
- byte 1 byte
User Defined data types:
In Java, user defined data types are created using "new" operator. For example, to create Employee object, the following instruction is coded
Employee employee = new Employee();
Now, the employee in the above statement, is a reference to an Employee class and called as user defined data type.
Now, coming to the point [Why Java is not secured..?], the primitive data types, long and double are not atomic variables. That means they are not executed in a single operation.
long variable's size is 64 bits. And it is divided into 32 bits + 32 bits while copying from one memory location to another memory location. and all user defined data types are based on primitive data types.
And these two primitive variables [long and double] breaks the atomicity law.
And developers can not bypass long and double variables in their program to create application. It is almost impossible to create the application without these variables.
Especially, in multi threaded based application, these non atomic variables may give unpredictable results.