Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What are an Atomic Variables in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Member
 
Join Date: Aug 2006
Posts: 253
What are an Atomic Variables in Java?
  

Hi friends,
I have recently completed some part in the threads section of Java programming language. Now I am stuck at the point where I have to define an atomic variables. I have never gone through this term. So I don't know anything about this.!! Hope that someone would know there.! Please tell me what are an Atomic Variables in Java? Help me soon.!!

Reply With Quote
  #2  
Old 19-02-2010
Member
 
Join Date: Mar 2008
Posts: 672
Re: What are an Atomic Variables in Java?

The java.util.concurrent.atomic package defines classes that support atomic operations on single variables. All classes have get and set methods that work like reads and writes on volatile variables. Knowing atomic operation is very important when we are writing thread operation in Java. Classes providing atomic access to main primitive types or to an object reference: AtomicBoolean, AtomicInteger and AtomicLong, AtomicReference.
Reply With Quote
  #3  
Old 19-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,844
Re: What are an Atomic Variables in Java?

Classes providing atomic access to fields in arrays of the correspoding types: AtomicIntegerArray, AtomicLongArray, AtomicReferenceArray. Classes to atomically couple a boolean or integer with a reference field: AtomicMarkableReference and AtomicStampedReference. Java Specification says that assignment to variables smaller than or equal to 32 bits is an atomic operation, which excludes variables of types double and long (both are 64 bits). So, the operation is atomic or not completely depends on type.
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: What are an Atomic Variables in Java?

To see how this package might be used, let's return to the Counter class we originally used to demonstrate thread interference :
Code:
class CounterDemo {
    private int c = 0;

    public void increment() {
        c++;
    }

    public void decrement() {
        c--;
    }

    public int value() {
        return c;
    }

}
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,946
Re: What are an Atomic Variables in Java?

One way to make Counter safe from thread interference is to make its methods synchronized, as in SynchronizedCounter is shown in the following example :
Code:
class SynchronizedCounterDemo {
    private int c = 0;

    public synchronized void increment() {
        c++;
    }

    public synchronized void decrement() {
        c--;
    }

    public synchronized int value() {
        return c;
    }

}
Reply With Quote
  #6  
Old 04-01-2011
javabuddy
 
Posts: n/a
Re: What are an Atomic Variables in Java?

Hi,

Only read and write operation on variable less than 32 bit are atomic in Java , so when you increment or decrement in Integer its not an atomic operation and can crate memory corruption problem. to solve this Java 5 adds concept of Atomic variables e.g. AtomicInteger, AtomicLong , AtomicBoolean etc on these variable certain operation can be perform atomically and that's why they are very useful in multithreaded applicaiton.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What are an Atomic Variables in Java?"
Thread Thread Starter Forum Replies Last Post
About instance variables in java Khaled11 Software Development 1 19-03-2010 02:38 PM
Java properties / variables dynamically Miles Runner Software Development 5 04-03-2010 10:31 AM
What is Instance variables in Java? Owen Fernandes Software Development 5 23-01-2010 07:27 AM
Multiplying Variables in java WinDoWLoCuS Software Development 3 03-12-2009 12:43 PM
Java bans Global Variables KALLIYAN Software Development 4 10-11-2009 03:17 AM


All times are GMT +5.5. The time now is 10:15 AM.