The Artima Developer Community
Sponsored Link

Java Buzz Forum
AutomicInteger in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
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
Reply to this message Reply

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:

  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.

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

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

Read: AutomicInteger in Java

Topic: JavaOne 2015 – Another Year, Another Step Forward Previous Topic   Next Topic Topic: Quick Web App Prototyping with Spring Boot & MongoDB

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use