Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Atomic operations

 
Mandan Happy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Am using an Array List inside a class like this
class A{
static ArrayList arrList=new ArrayList();
static void LengthyFunction(){
//Many Lines of code...
//.....
Object o=arrList.get(id)// statement
}
}
}
Will the jvm consider the above statement as an atomic operation? In other words Do I have to have the above statement in a synchronized block? I cant use synchronized(this) in a static context and also I dont want to synchronize the whole funtion since the function is a bit lengthy one.
The Big Question is What operations are considered as atomic?
Thanks in Advance
Mandan
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a 32 bit machine, all operations on 32 bits or less are atomic. Therefore, all operations on primitive types, except double and long, and operations on object references are atomic.
The primitives are represented as 32 bits, except double and long which are represented with 64 bits. Object references are native pointers and are 32 bits as well.
Note however, that atomicity does not imply thread safety. Atomic operations are indivisible, but not necessarily thread safe. JVMs allow threads to keep private working copies of variables separate from main memory. The variables are reconciled at specific points. If you don't want to synchronize the entire method, you can wrap the statement with a synchronized block as follows:

The synchronized block ensures reconciliation.
Peter Haggar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic