File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Atomic operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Atomic operations" Watch "Atomic operations" New topic

Atomic operations

Mandan Happy
Ranch Hand

Joined: Dec 17, 2001
Posts: 65
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
Peter Haggar
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
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

Senior Software Engineer, IBM
author of: Practical Java
I agree. Here's the link:
subject: Atomic operations
It's not a secret anymore!