instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
instanceof java is a java related one.
AtomicInteger in Java
Posted: Dec 27, 2015 12:26 PM
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: AtomicInteger in Java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java
Advertisement
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:
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:
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 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:
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:
Read: AtomicInteger in Java