Easy learning with example program codes

atomiclong in java

The java.util.concurrent.atomic.AtomicLong class provides a long variable which can be read and written atomically.

Creating an AtomicLong

AtomicLong atomicLong = new AtomicLong();

Create an atomic long with the default value 0.

AtomicLong atomicLong = new AtomicLong(453);

Create an atomic long with the initial value 453.

Commonly used methods of AtomicLong class

  • get(): Reads the value atomically.
  • set(long newValue): Writes the value atomically.
  • compareAndSet(long expect, long update): Variable value is compared to the expect param, and if they are equal, then the value is set to the update param and true is returned.
  • addAndGet(long delta): The delta is added to the value and the new value is returned.
  • getAndAdd(long delta): It adds the delta to the value, and returns the previous value.
  • getAndIncrement(): The value is incremented, and its previous value is returned.
  • incrementAndGet(): The value is incremented and its new value is returned.
  • decrementAndGet(): The value is decremented and its new value is returned.
  • getAndDecrement(): The value is decremented and its previous value is returned.


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class AtomicLongTest {
  private static AtomicLong result = new AtomicLong();
  public static void main(String[] args) throws InterruptedException {
	for (int i = 0; i < 5; i++) {
          ExecutorService es = Executors.newFixedThreadPool(50);
          for (int j = 1; j <= 20; j++) {
              int finalI = j;
              es.execute(() -> {
            	  result.addAndGet((long) Math.pow(2, finalI));
          es.awaitTermination(10, TimeUnit.MINUTES);



Related topics

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki