1

In the book, it has the following example, it only sync the insert, but didn't sync lookup.I knew the currentIndex will point to a different object after insert, but is the operation of pointing to a different object atomic? example, suppose the pointer is of 8 bytes (on 64 bit machine), then at some point currentIndex will switch the pointer, but if that switch is not atomic, such as it first switch the first 4 bytes, and then switch the second 4 bytes, and if between the first and second switch, we lookup the data, then the pointer points to an object never exists and that will cause problem

class ImmutableService[Key, Value] extends Service[Key, Value] {
   var currentIndex = new ImmutableHashMap[Key,Value]
   def lookUp(k: Key): Option[Value] = currentIndex.get(k)
   def insert(k: Key, v: Value): Unit = synchronized {
       currentIndex = currentIndex + ((k, v))
   }
}
Daniel Wu
  • 5,853
  • 12
  • 42
  • 93
  • 1
    From http://stackoverflow.com/questions/4756536/what-operations-in-java-are-consider-atomic ,and elsewhere, all assignments are considered atomic. – Chick May 14 '13 at 03:44

1 Answers1

0

Yes, currentIndex is a reference - so changes to it will be done atomically.

Check:

Are 64 bit assignments in Java atomic on a 32 bit machine?

Community
  • 1
  • 1
Sandipan
  • 48
  • 5