Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Reference variable access  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!