» » AtomicInteger in Java


  • Java.util.concurrent.atomic package provides very useful classes that support lock free and thread safe programming.
  • The main use of this class is an int value that may be updated automatically.
  • AtomicInteger has some useful methods. Before that lets see the some points about this class.
  • Commonly we will use this AtomicInteger to handle the counter that is accessible by different threads simultaneously.

Java.util.concurrent.atomic.AtomicInteger:

  1. public class AtomicInteger
  2. extends Number
  3. implements Serializable

AtomicInteger Class Constructors:


  • public AtomicInteger(): Creates a new AtomicInteger object with default value 0.
  1. AtomicInteger atomicInteger = new AtomicInteger();
  • public AtomicInteger(int initialValue): Creates a new AtomicInteger object with given initial  value.
  1. AtomicInteger atomicInteger = new AtomicInteger(10);


 AtomicInteger Class Methods:


 1.public final void set(int newValue):
  •  Sets given value to the object.

  Java Program to create AtomicInteger class object and sets some value.

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.   System.out.println(atomicInteger);
  12.   atomicInteger.set(10);
  13.   System.out.println(atomicInteger);
  14.  
  15. }
  16.  
  17. }

Output:

  1. 0
  2. 10

2, public final void get():

  • Used to get current value.

Java Program to create AtomicInteger class object and sets some value and get.

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.   System.out.println(atomicInteger.get());
  12.   atomicInteger.set(10);
  13.   System.out.println(atomicInteger.get());
  14.  
  15. }
  16.  
  17. }

Output:

  1. 0
  2. 10
3.public final int getAndSet(int newValue):
  • Automatically sets the given value and returns old value.

  Java Program which explains getAndSet(int x) method of AtomicInteger class

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.    System.out.println(atomicInteger.get());
  12.  
  13.    atomicInteger.set(10);
  14.  
  15.    System.out.println(atomicInteger.get());
  16.  
  17.    System.out.println(atomicInteger.getAndSet(12));
  18.  
  19.    System.out.println(atomicInteger.get());
  20.  
  21. }
  22.  
  23. }

Output:

  1. 0
  2. 10
  3. 10
  4. 12



4.public final int incrementAndGet()
  • Automatically increments the value one and returns updated value

  Java Program which explains incrementAndGet() method of AtomicInteger class

  1. package com.instaceofjava;
  2.  
  3. import java.util.concurrent.atomic.AtomicInteger;
  4.  
  5. public class AtomicIntegerExample{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. AtomicInteger atomicInteger = new AtomicInteger();
  10.  
  11.    System.out.println(atomicInteger.get());
  12.  
  13.    atomicInteger.set(10);
  14.  
  15.    System.out.println(atomicInteger.get());
  16.  
  17.    System.out.println(atomicInteger.incrementAndGet());
  18.  
  19.  
  20. }
  21.  
  22. }

Output:

  1. 0
  2. 10
  3. 11


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu