aspose file tools*
The moose likes Threads and Synchronization and the fly likes Atomic operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Atomic operation " Watch "Atomic operation " New topic
Author

Atomic operation

Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 58
In the below link,

http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

there is a sentence

Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).


Can some one be kind enough to tell me why is there a exception to long and double primitive data types.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Rajesh Nagaraju wrote:In the below link,

http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

there is a sentence

Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).


Can some one be kind enough to tell me why is there a exception to long and double primitive data types.


Because double and long are 8 bytes length (64-bits),
and 16-bit processors have only instructions that operate on maximum 32-bits operands, but not on 64-bits.
In other words - 16-bit processor can read or write 32-bits from/to memory in one operation, but for variables that are 64-bits long requires more than 1 operation.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
16-bit processors

I don't think any of those can run a modern JVM :-) Most processors these days are 64 bit processors, with some 32 bit ones left over from a few years ago. Whether or not any given CPU can handle 64 bit chunks of data depends on the CPU; most can.

I think the limitation is a leftover from the days where the JVM could only handle 32 bit quantities, and would thus need several steps to handle 64 bit data types (and not just a single one, which would be atomic). I'm not sure if modern 64 bit JVMs are changing anything about this; the Java Language Specification might say something about that.
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 58
Thanks a lot for your replies

It was very helpful.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

Rajesh Nagaraju wrote:
Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).


Can some one be kind enough to tell me why is there a exception to long and double primitive data types.



Also, if the variables are declared as volatile, then reads and writes are atomic, even for long and double primatives types.... or basically, you can regard it as atomic for everything; as if you don't declare it as volatile, the variables may be cached, which is not good if you are trying to use the variables without synchronization.

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 58
volatile for long and double are treated as atomic, if in the 16 bit processor still we cannot read it in 1 operation..
How does the JVM manage to maintain it atomic?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rajesh Nagaraju wrote:volatile for long and double are treated as atomic, if in the 16 bit processor still we cannot read it in 1 operation..
How does the JVM manage to maintain it atomic?


Because our Java code doesn't run on the hardware CPU. It runs in the JVM. Even in a 32-bit processor, doubles and longs are two words. The JVM controls the execution of our instructions, so it can make sure that our instructions always see certain operations as atomic, even if the hardware doesn't support that. Just like we can used the synchronized keyword to make long, complex operations atomic in our code, so can the JVM use the underlying native API's multithreading features to make its operations atomic.

At the bare minimum, all that is required is that the hardware provides an atomic test-and-set operation, or some equivalent mutual exclusion feature. If that's present, anything can be made atomic. If it's not, nothing can.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

Rajesh Nagaraju wrote:volatile for long and double are treated as atomic, if in the 16 bit processor still we cannot read it in 1 operation..
How does the JVM manage to maintain it atomic?



For 64 bit JVMs, read and writes of doubles and longs are naturally atomic. For 32 bit JVMs, an internal lock is used.

Henry
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 58
Thank you, marking this thread resolved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Atomic operation