Results 1 to 6 of 6

Thread: What are an Atomic Variables in Java?

  1. #1
    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.!!

  2. #2
    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    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.

  4. #4
    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;
        }
    
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    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;
        }
    
    }

  6. #6
    javabuddy Guest

    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.

Similar Threads

  1. About instance variables in java
    By Khaled11 in forum Software Development
    Replies: 1
    Last Post: 19-03-2010, 02:38 PM
  2. Java properties / variables dynamically
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 11:31 AM
  3. What is Instance variables in Java?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 23-01-2010, 08:27 AM
  4. Multiplying Variables in java
    By WinDoWLoCuS in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 01:43 PM
  5. Java bans Global Variables
    By KALLIYAN in forum Software Development
    Replies: 4
    Last Post: 10-11-2009, 04:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,657,536.92644 seconds with 17 queries