- 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:
- public class AtomicInteger
- extends Number
- implements Serializable
AtomicInteger Class Constructors:
- public AtomicInteger(): Creates a new AtomicInteger object with default value 0.
- AtomicInteger atomicInteger = new AtomicInteger();
- public AtomicInteger(int initialValue): Creates a new AtomicInteger object with given initial value.
- 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.
- package com.instaceofjava;
- import java.util.concurrent.atomic.AtomicInteger;
- public class AtomicIntegerExample{
- public static void main(String[] args) {
- AtomicInteger atomicInteger = new AtomicInteger();
- System.out.println(atomicInteger);
- atomicInteger.set(10);
- System.out.println(atomicInteger);
- }
- }
Output:
- 0
- 10
2, public final void get():
- Used to get current value.
Java Program to create AtomicInteger class object and sets some value and get.
- package com.instaceofjava;
- import java.util.concurrent.atomic.AtomicInteger;
- public class AtomicIntegerExample{
- public static void main(String[] args) {
- AtomicInteger atomicInteger = new AtomicInteger();
- System.out.println(atomicInteger.get());
- atomicInteger.set(10);
- System.out.println(atomicInteger.get());
- }
- }
Output:
- 0
- 10
- Automatically sets the given value and returns old value.
Java Program which explains getAndSet(int x) method of AtomicInteger class
- package com.instaceofjava;
- import java.util.concurrent.atomic.AtomicInteger;
- public class AtomicIntegerExample{
- public static void main(String[] args) {
- AtomicInteger atomicInteger = new AtomicInteger();
- System.out.println(atomicInteger.get());
- atomicInteger.set(10);
- System.out.println(atomicInteger.get());
- System.out.println(atomicInteger.getAndSet(12));
- System.out.println(atomicInteger.get());
- }
- }
Output:
- 0
- 10
- 10
- 12
4.public final int incrementAndGet()
- Automatically increments the value one and returns updated value
Java Program which explains incrementAndGet() method of AtomicInteger class
- package com.instaceofjava;
- import java.util.concurrent.atomic.AtomicInteger;
- public class AtomicIntegerExample{
- public static void main(String[] args) {
- AtomicInteger atomicInteger = new AtomicInteger();
- System.out.println(atomicInteger.get());
- atomicInteger.set(10);
- System.out.println(atomicInteger.get());
- System.out.println(atomicInteger.incrementAndGet());
- }
- }
Output:
- 0
- 10
- 11
No comments