Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of java primitives on 32/64 bit jvm?

Tags:

  1. The size of int is 32 bits and long/double are 64 bits. Will these sizes remain same on
    1. 32/64 bit JVM
    2. 32/64 bit processors
  2. if yes then will the operations on long/double be atomic on a 64 bit processor/JVM?

The Oracle Java tutorial says:

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

Does this statement has anything to do with jvm/processor architecture? Can someone please explain.

3. Finally will i be able to make read/write of double/long atomic if i use 64 bit jvm and processor

like image 607
Rekha Avatar asked Mar 01 '12 06:03

Rekha


1 Answers

Yes, the sizes are the same on both 32-bit and 64-bit JVM. Assignment is not guaranteed to be atomic for either long or double in Java. Atomic assignment, is still no guarantee of visibility from another thread. Because threads are allowed to 'shadow' variables in memory, even atomic assignment to a variable does not necessarily write-thru to main memory (but when main memory is updated, it will be done so atomically). You must always use synchronization barriers of some kind when accessing shared state from two or more threads if you expect one thread to consistently see changes by the other.

like image 185
brettw Avatar answered Sep 29 '22 13:09

brettw