instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
instanceof java is a java related one.
AutomicInteger in Java
Posted: Dec 26, 2015 5:29 AM
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: AutomicInteger 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.1. 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. 2. 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:
Read: AutomicInteger in Java