Close

Java - AtomicReference Example

[Updated: Jul 23, 2018, Created: May 22, 2018]

AtomicReference<V> is an object reference that may be updated atomically.

Examples

AtomiceReference of Double

Since there's no class like AtomicDouble, we can use AtomicReference to wrap the Double.

package com.logicbig.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceExample {
  private static AtomicReference<Double> sum = new AtomicReference<>();

  public static void main(String[] args) throws InterruptedException {
      for (int k = 0; k < 5; k++) {
          sum.set(0d);
          ExecutorService es = Executors.newFixedThreadPool(50);
          for (int i = 1; i <= 50; i++) {
              int finalI = i;
              es.execute(() -> {
                  sum.accumulateAndGet(Math.pow(1.5, finalI),
                          (d1, d2) -> d1 + d2);
              });
          }
          es.shutdown();
          es.awaitTermination(10, TimeUnit.MINUTES);
          System.out.println(sum.get());
      }
  }
}
1.9128644976421487E9
1.9128644976421487E9
1.9128644976421487E9
1.9128644976421487E9
1.9128644976421487E9

Without AtomicReference

package com.logicbig.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class NoAtomicReferenceExample {
  private static Double sum;

  public static void main(String[] args) throws InterruptedException {
      for (int k = 0; k < 5; k++) {
          sum=0d;
          ExecutorService es = Executors.newFixedThreadPool(50);
          for (int i = 1; i <= 50; i++) {
              int finalI = i;
              es.execute(() -> {
                  sum+=Math.pow(1.5, finalI);
              });
          }
          es.shutdown();
          es.awaitTermination(10, TimeUnit.MINUTES);
          System.out.println(sum);
      }
  }
}
1.9128638628787515E9
1.9128644976421487E9
1.9128476635299528E9
1.9128644976421487E9
1.8879854999200337E9

The result is not always same.

AtomicReference of BigDecimal

package com.logicbig.example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceExample2 {
  private static AtomicReference<BigDecimal> sum = new AtomicReference<>();

  public static void main(String[] args) throws InterruptedException {
      for (int k = 0; k < 5; k++) {
          sum.set(BigDecimal.ZERO);
          ExecutorService es = Executors.newFixedThreadPool(50);
          for (int i = 1; i <= 50; i++) {
              int finalI = i;
              es.execute(() -> {
                  sum.accumulateAndGet(new BigDecimal(1.5).pow(finalI),
                          (bg1, bg2) -> bg1.add(bg2));
              });
          }
          es.shutdown();
          es.awaitTermination(10, TimeUnit.MINUTES);
          System.out.println(sum.get().setScale(2, RoundingMode.CEILING));
      }
  }
}
1912864497.65
1912864497.65
1912864497.65
1912864497.65
1912864497.65

Example Project

Dependencies and Technologies Used:

  • JDK 10
  • Maven 3.3.9

AtomicReference Example Select All Download
  • atomic-reference-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • AtomicReferenceExample2.java

    See Also