• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference variable access

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following scenario, where x & y are ref variables, and A and B are objects:

x->A
y->B

If I now set x=y, what happens if a piece of code tries to use x *whilst* the address of B is being copied to it?

Perhaps this is an OS level issues rather than JVM, any advice appreciated.

Thanks in advance!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Reads and writes to reference variables, as well as to all variables of four bytes or fewer are guaranteed by the Java language spec to be atomic -- in other words, no other thread will see such a variable in a partially-assigned state. The same is not true for 8-byte variables of types double and long.

There are other issues besides consistency to consider; in the absence of synchronization, if one thread writes to a variable, others may not immediately see the new value, but will continue to see the old.
 
Eddie Menuek
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your answer, exactly what I wanted to hear

Do you by any chance have a link to an online spec that I can quote in my design decisions doc?

Thanks again!
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS would be a good place to start. Keep in mind the memory model changed drastically for the better in the latest edition, which applies to 1.5+.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS is the Java Language Specification, in case that wasn't clear.

Also the Java Virtual Machine Specification might be of interest.
[ July 13, 2006: Message edited by: Jesper Young ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic