Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference variable access

 
Eddie Menuek
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!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
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!
 
Ken Blair
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+.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic