Implementation of custom/own AtomicLong in java



In this thread concurrency tutorial we will learn how to implement custom/own AtomicLong in java with program and examples. In previous tutorial we read how to use AtomicLong in java. This tutorial intends you provides you basic functionality of AtomicLong using your own java code

We will write java code for important methods of your custom/own AtomicLong like getAndSet, compareAndSet, incrementAndGet, getAndAdd, getAndIncrement, decrementAndGet, getAndDecrement and how to use them in thread concurrency in java.

Contents of page :
  • 1) What is AtomicLongCustom in java ?
  • 1.1) AtomicLongCustom constructors in java >
  • AtomicLongCustom()
  • AtomicLongCustom(long initialValue)
  • 1.2) AtomicLongCustom important Methods in java >
  • long get()
  • void set(long newValue)
  • long getAndSet(long newValue)
  • boolean compareAndSet(long expect, long update)
  • 1.3) Addition methods in java >
  • long addAndGet(long value)
  • long incrementAndGet()
  • long getAndAdd(long value)
  • long getAndIncrement()
  • 1.4) Subtraction methods in java >
  • long decrementAndGet()
  • long getAndDecrement()
  • 2) Custom AtomicLong’s code in java >
  • 3) Program to demonstrate usage of AtomicLongCustom in java >
  • Occasionally output may be different in java >

1) What is AtomicLongCustom in java ?
In previous tutorial we read how to use AtomicLong in java. In this post we will be implementing custom AtomicLong. This post intends you give you basic functionality of AtomicLong using your own java code

AtomicLongCustom  provides you with long value that is updated atomically. i.e. we can use these classes without any explicit synchronization in multithreading environment, because any operation done on these classes is thread safe.


1.1) AtomicLongCustom constructors in java >
  • AtomicLongCustom()
Creates a new AtomicLongCustom and is initialized to 0.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom();
We have created a new AtomicLongCustom and it is initialized to 0.

  • AtomicLongCustom(long initialValue)
Creates a new AtomicLongCustom and is initialized to specified initialValue.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
We have created a new AtomicLongCustom and it is initialized to 11.



1.2) AtomicLongCustom important Methods in java >
  • long get()
method returns the current value

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.get();
Method will return 11.

  • void set(long newValue)
Sets to newValue.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.set(12);
Method will set return AtomicLongCustom to 12.

  • long getAndSet(long newValue)
Sets to newValue and returns the old value.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.getAndSet(12);
Method will set return AtomicLongCustom to 12. And return 11.

  • boolean compareAndSet(long expect, long update)
Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.compareAndSet(11, 12);
Now, in call to  compareAndSet method first parameter [i.e. 11] is equal to original value, so method will set AtomicLongCustom to 12.
And returns true if value was successfully set.

1.3) Addition methods in java >
  • long addAndGet(long value)
adds value to the current value. And return updated value.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.addAndGet(4);
adds 4 to the current value. And return 15.

  • long incrementAndGet()
increments current value by 1. And return updated value.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.incrementAndGet();
increments current value by 1. And return 12.

  • long getAndAdd(long value)
Method return current value. And adds value to the current value.
Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.getAndAdd(4);
Method return 11. And adds 4 to 11.

  • long getAndIncrement()
Method return current value. And increments current value by 1.
Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.getAndIncrement();
Method return 11. And increments 11 by 1.


1.4) Subtraction methods in java >
  • long decrementAndGet()
decrements current value by 1. And return updated value.

Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.decrementAndGet();
decrements current value by 1. And return 10.

  • long getAndDecrement()
Method return current value. And decrements current value by 1.
Example >
AtomicLongCustom atomicLongCustom =new AtomicLongCustom(11);
atomicLongCustom.getAndDecrement();
Method return 11. And decrements 11 by 1.


2) Custom AtomicLong’s code in java >
/**
* @author AnkitMittal
* Copyright (c), AnkitMittal .
* All Contents are copyrighted and must not be reproduced in any form.
*
* AtomicLongCustom provides you with long value that is updated atomically.
*/
class AtomicLongCustom{
   long currentValue;
   long previousValue;
  
   //AtomicLongCustom constructors >
   /**
   * Creates a new AtomicLongCustom and is initialized to 0.
   */
   public AtomicLongCustom(){
          currentValue=0;
   }
  
   /**
   * Creates a new AtomicLongCustom and is initialized to specified initialValue.
   * @param initialValue
   */
   public AtomicLongCustom(long initialValue){
          currentValue=initialValue;
   }
  
   //AtomicLongCustom important Methods >
   /**
   * method returns the current value
   *
   */
   public synchronized long get(){
          return currentValue;
   }
  
   /**
   * Sets to newValue.
   */
   public synchronized void set(long newValue){
          currentValue=newValue;
   }
  
   /**
   * Sets to newValue and returns the old value.
   */
   public synchronized long getAndSet(long newValue){
          previousValue=currentValue;
          currentValue=newValue;
          return previousValue;
   }
  
   /**
   * Compare with expect, if equal, set to update and return true.
   */
   public synchronized boolean compareAndSet(long expect, long update){
          if(currentValue == expect){
                 currentValue=update;
                 return true;
          }
          else
                 return false;
   }
  
   //Addition methods >
   /**
   * adds value to the current value. And return updated value.
   */
   public synchronized long addAndGet(long value){
          return currentValue+=value;
   }
  
   /**
   * increments current value by 1. And return updated value.
   */
   public synchronized long incrementAndGet(){
          return ++currentValue;
   }
  
   /**
   * Method return current value. And adds value to the current value.
   */
   public synchronized long getAndAdd(long value){
          previousValue=currentValue;
          currentValue+=value;
          return previousValue;
   }
  
   /**
   * Method return current value. And increments current value by 1.
   *
   */
   public synchronized long getAndIncrement(){
          return currentValue++;
   }
  
   //Subtraction methods >
   /**
   * decrements current value by 1. And return updated value.
   */
   public synchronized long decrementAndGet(){
          return --currentValue;
   }
  
   /**
   * Method return current value. And decrements current value by 1.
   */
   public synchronized long getAndDecrement(){
          return currentValue--;
   }
  
   @Override
   public String toString() {
          return "AtomicLongCustom= " + currentValue ;
   }  
}

3) Program to demonstrate usage of AtomicLongCustom in java >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* @author AnkitMittal
* Copyright (c), AnkitMittal .
* All Contents are copyrighted and must not be reproduced in any form.
*
* AtomicLongCustom provides you with long value that is updated atomically.
*/
class AtomicLongCustom{
   long currentValue;
   long previousValue;
  
   //AtomicLongCustom constructors >
   /**
   * Creates a new AtomicInteger and is initialized to 0.
   */
   public AtomicLongCustom(){
          currentValue=0;
   }
  
   /**
   * Creates a new AtomicInteger and is initialized to specified initialValue.
   * @param initialValue
   */
   public AtomicLongCustom(long initialValue){
          currentValue=initialValue;
   }
  
   //AtomicLongCustom important Methods >
   /**
   * method returns the current value
   *
   */
   public synchronized long get(){
          return currentValue;
   }
  
   /**
   * Sets to newValue.
   */
   public synchronized void set(long newValue){
          currentValue=newValue;
   }
  
   /**
   * Sets to newValue and returns the old value.
   */
   public synchronized long getAndSet(long newValue){
          previousValue=currentValue;
          currentValue=newValue;
          return previousValue;
   }
  
   /**
   * Compare with expect, if equal, set to update and return true.
   */
   public synchronized boolean compareAndSet(long expect, long update){
          if(currentValue == expect){
                 currentValue=update;
                 return true;
          }
          else
                 return false;
   }
  
   //Addition methods >
   /**
   * adds value to the current value. And return updated value.
   */
   public synchronized long addAndGet(long value){
          return currentValue+=value;
   }
  
   /**
   * increments current value by 1. And return updated value.
   */
   public synchronized long incrementAndGet(){
          return ++currentValue;
   }
  
   /**
   * Method return current value. And adds value to the current value.
   */
   public synchronized long getAndAdd(long value){
          previousValue=currentValue;
          currentValue+=value;
          return previousValue;
   }
  
   /**
   * Method return current value. And increments current value by 1.
   *
   */
   public synchronized long getAndIncrement(){
          return currentValue++;
   }
  
   //Subtraction methods >
   /**
   * decrements current value by 1. And return updated value.
   */
   public synchronized long decrementAndGet(){
          return --currentValue;
   }
  
   /**
   * Method return current value. And decrements current value by 1.
   */
   public synchronized long getAndDecrement(){
          return currentValue--;
   }
  
   @Override
   public String toString() {
          return "AtomicLongCustom= " + currentValue ;
   }  
}
/**
* Main class
*/
public class AtomicLongCustomTest {
  
   //Create a new AtomicLongCustom and is initialized with 0.
   static AtomicLongCustom sharedAtomicLongCustomCustom =new AtomicLongCustom();
  
   public static void main(String...args) throws InterruptedException{
          MyRunnable runnable=new MyRunnable();
          Thread thread1=new Thread(runnable,"Thread-1");
          Thread thread2=new Thread(runnable,"Thread-2");
          thread1.start();
          thread2.start();
         
          Thread.sleep(1000); //delay to ensure Thread-1 and Thread-2 finish
          System.out.println("After completion of both threads, "
                       + "sharedAtomicLongCustom = "+sharedAtomicLongCustomCustom);
  
   }
  
}
class MyRunnable implements Runnable{
  
   public void run(){
          for(int i=0;i<2;i++){
                 System.out.println("ThreadName="+Thread.currentThread().getName()
                              +" > "+
                      AtomicLongCustomTest.sharedAtomicLongCustomCustom.incrementAndGet());
          }         
         
   }
}
/*OUTPUT
ThreadName=Thread-1 > 1
ThreadName=Thread-2 > 2
ThreadName=Thread-1 > 3
ThreadName=Thread-2 > 4
After completion of both threads, sharedAtomicLongCustom = 4
*/
/*OUTPUT
ThreadName=Thread-2 > 1
ThreadName=Thread-2 > 3
ThreadName=Thread-1 > 2
ThreadName=Thread-1 > 4
After completion of both threads, sharedAtomicLongCustom = 4
*/

In the program, a static AtomicLongCustom is created with name sharedAtomicLongCustom and is initialized to 0.  Then, Thread-1 and Thread-2 atomically increments sharedAtomicLongCustom  inside run( ) method using incrementAndGet() method [incrementAndGet() method increments current value by 1. And return updated value].
Here, sharedAtomicLongCustom  being AtomicLongCustom prevents two threads from writing to it at the same time.

Occasionally output may be different in java >
/*OUTPUT
ThreadName=Thread-2 > 1
ThreadName=Thread-2 > 3
ThreadName=Thread-1 > 2
ThreadName=Thread-1 > 4
After completion of both threads, sharedAtomicLongCustom = 4
*/

sharedAtomicLongCustom is incremented atomically, but sysout statements have executed out of order.


In this thread concurrency tutorial we learned how to implement custom/own AtomicLong in java with program and examples. In previous tutorial we read how to use AtomicLong in java. This tutorial was provided you basic functionality of AtomicLong using your own java code

We wrote java code for important methods of your custom/own AtomicLong like getAndSet, compareAndSet, incrementAndGet, getAndAdd, getAndIncrement, decrementAndGet, getAndDecrement and how to use them in thread concurrency in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>


Atomic operations >

Atomic operations in java


AtomicInteger in java

Implementation of custom/own AtomicInteger in java


AtomicLong in java

Implementation of custom/own AtomicLong in java


AtomicBoolean in java




BlockingQueue and LinkedBlockingQueue  >

Solve Consumer Producer problem by using BlockingQueue in multithreading

Custom implementation of LinkedBlockingQueue class which implements BlockingQueue interface




eEdit
Must read for you :