Parallel Sorting, Exact Numeric Operations and Stamped Lock in Java 8

Introduction
  

This article covers the new Parallel Sorting, Exact Numeric Operations and Stamped Lock features, of Java 8.  The Array class of the "util" package contains various methods but, "parallelSort()" is one of the new features.  Similarly, the Math class of the "lang" package contains various methods, but to get an exact value of multiplication of large data, this class has a "multiplyExact()" method.  The class "locks" of the "util" package contains some new features.  The "StampedLock()" method is one of them and these methods are described, in this article.

 
Parallel Sorting

 
parallelSort() is a new method of the Array class.  As the name suggests, this method sorts in parallel.  It uses initial values the size of array, is lesser than the initial value that is sorted, by sequential sorting.  In this, it divides the array into 4 modules and sorts the first two, then merges them.  Then it repeats that procedure.  When the length of the module reaches MIN, then it starts sorting using an appropriate "Array.sort()" methodThis feature gives faster results.  Let's see an example.
Example: In this example, first create a list and declare some numbers.  Then, create two arrays for sorting:
  1. import java.util.*;  
  2. public class Parallel_Sort {  
  3.  public static void main(String[] args) {  
  4.   List < Integer > list = new ArrayList();  
  5.   Random r = new Random();  
  6.   for (int x = 0; x < 27061990; x++) {  
  7.    list.add(r.nextInt(2706));  
  8.   }  
  9.   Integer[] array1 = new Integer[list.size()];  
  10.   Integer[] array2 = new Integer[list.size()];  
  11.   array1 = list.toArray(array1);  
  12.   array2 = list.toArray(array2);  
  13.   long start = System.currentTimeMillis();  
  14.   Arrays.sort(array1);  
  15.   long end = System.currentTimeMillis();  
  16.   System.out.println("Array Sort Required Time: " + (end - start));  
  17.   start = System.currentTimeMillis();  
  18.   Arrays.parallelSort(array2);  
  19.   end = System.currentTimeMillis();  
  20.   System.out.println("Parallel Sort Required Time: " + (end - start));  
  21.  }  
  22. }  
Output 
  
 

Exact Numeric Operations
 

The "math" package also has some features in the new version of Java.  Decimal Arithmetic is of the oldest techniques and has been refined over the decades.  The methods of the "math" class throw "java. lang.AirthmeticExceptios" every time the result goes to a max limit, or overflows.  Let's see an example.  If we multiply 100000*100000 it generally gives us wrong output.  But, by using the "multiplyExact()" method, we can get over this problem because, it throws "java. lang.AirthmeticExceptios" if the result overflows the max limit.  Let's see the example.
Example
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class oldmath_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println(100000 * 100000);  
  7.  }  
  8. }   
Output
 
Example
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class math_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println(Math.multiplyExact(100000100000));  
  7.  }  
  8. }   
Output
 
 
Another example
 
We all know about n%2 (using the modulus operator), if the number is even it gives 0.  If the number is odd it gives 1.  But, the problem starts when a negative even number gives -1.
Example 
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class oldmath_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println(-27 % 2);  
  7.   System.out.println(-26 % 2);  
  8.   System.out.println(50 % 2);  
  9.   System.out.println(51 % 2);  
  10.  }  
  11. }  
Output
 
Example: But by using "Math.floorMod()" method we can handle this problem.
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class math_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println(Math.floorMod(-262));  
  7.   System.out.println(Math.floorMod(-272));  
  8.   System.out.println(Math.floorMod(502));  
  9.   System.out.println(Math.floorMod(512));  
  10.  }  
  11. }   
Output
 
 
Another example
 
Similarly, if we work good (position number) is calculated as (position + adjustment) but, what happens when it gives negative numbers?  
 
Example
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class oldmath_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println((8 + 2) % 8);  
  7.   System.out.println((5 + 8) % 8);  
  8.   System.out.println((5 - 24) % 8);  
  9.  }  
  10. }   
Output
 
 
Example: Also, by using the "Math.floorMod()" method, we can handle this problem.
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class math_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println(Math.floorMod(8 + 28));  
  7.   System.out.println(Math.floorMod(5 + 88));  
  8.   System.out.println(Math.floorMod(5 - 248));  
  9.  }  
  10. }   
Output
 
 
Example:
This is the example of the "nextDown()" method.
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class nxtdwn_exmple {  
  5.  public static void main(String args[]) throws java.lang.ArithmeticException {  
  6.   System.out.println(Math.nextDown(1000));  
  7.   System.out.println(Math.nextDown(1.27));  
  8.  }  
  9. }   
Output: Please note that only the "Math.nextDown()" method is added in Java 8.  The "Math.nextUp()" method already existed in Java 6.
 
 

Stamped Lock

Stamped Lock is based on the capability lock for controlling read/write access.  It has three modes Writing, Reading and Optimistic Reading.  It has a version and a mode state.  A Stamp describes, and access, the control of the lock state and can be obtained by the Lock acquisition method.  The Lock and conversion methods require stamps, as arguments, but fail if they don't match the state of the lock.  "Try", returns the special value zero to describe failure to get access.
Example
  1. import java.util.*;  
  2. import java.util.concurrent.locks.*;  
  3. public class StampedLock_Demo {  
  4.  private final StampedLock sl = new StampedLock();  
  5.  private long bal;  
  6.  public StampedLock_Demo(long bal) {  
  7.   this.bal = bal;  
  8.  }  
  9.  public void deposit(long amt) {  
  10.   long stamp = sl.writeLock();  
  11.   try {  
  12.    bal = bal + amt;  
  13.   } finally {  
  14.    sl.unlockWrite(stamp);  
  15.   }  
  16.  }  
  17.  public void withdraw(long amt) {  
  18.   long stamp = sl.writeLock();  
  19.   try {  
  20.    bal = bal - amt;  
  21.   } finally {  
  22.    sl.unlockWrite(stamp);  
  23.   }  
  24.  }  
  25.  public long getBalance() {  
  26.   long stamp = sl.readLock();  
  27.   try {  
  28.    return bal;  
  29.   } finally {  
  30.    sl.unlockRead(stamp);  
  31.   }  
  32.  }  
  33.  public long getBalanceOptimisticRead() {  
  34.   long stamp = sl.tryOptimisticRead();  
  35.   long bal = this.bal;  
  36.   if (!sl.validate(stamp)) {  
  37.    stamp = sl.readLock();  
  38.    try {  
  39.     bal = this.bal;  
  40.    } finally {  
  41.     sl.unlockRead(stamp);  
  42.    }  
  43.   }  
  44.   return bal;  
  45.  }  
  46. }  
 

Summary

The summarization of this article is, that the new features of Java 8 are very useful for implementation.  They help programmers to get output to the program faster and more precisely.  These features will take Java to th next level.