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 contain 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 repeats that procedure. When the length of the module reaches MIN then it starts sorting using an appropriate "Array.sort()" method. This 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. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         // List with random numbers  
  7.         List<Integer> list = new ArrayList();  
  8.         Random r = new Random();  
  9.         for (int x = 0; x < 27061990; x++)  
  10.         {  
  11.             list.add(r.nextInt(2706));  
  12.         }  
  13.         // Create two arrays  
  14.         Integer[] array1 = new Integer[list.size()];  
  15.         Integer[] array2 = new Integer[list.size()];  
  16.         array1 = list.toArray(array1);  
  17.         array2 = list.toArray(array2);  
  18.         //By using array sort  
  19.         long start = System.currentTimeMillis();  
  20.         Arrays.sort(array1);  
  21.         long end = System.currentTimeMillis();  
  22.         System.out.println("Array Sort Required Time: " +  (end - start));  
  23.         //By using parallel sort  
  24.         start = System.currentTimeMillis();  
  25.         Arrays.parallelSort(array2);  
  26.         end = System.currentTimeMillis();  
  27.         System.out.println("Parallel Sort Required Time: " +  (end - start));  
  28.     }  
  29. }  

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. Generally it gives us wrong output but by using the "multiplyExact()" method we can get over this problem and 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. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println(100000 * 100000);  
  9.     }  
  10. }  

Output

 

Example

  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.lang.Math;  
  4. public class math_exmple  
  5. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println(Math.multiplyExact(100000 , 100000));  
  9.     }  
  10. }  

Output

 
 
Another example
 
We all know about n%2 (using the modulus operator), if the number is even its gives 0 and if the number is odd its 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. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println( -27 % 2 );  
  9.         System.out.println( -26 % 2 );  
  10.         System.out.println( 50 % 2 );  
  11.         System.out.println( 51 % 2 );  
  12.     }  
  13. }  

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. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println( Math.floorMod(-26 , 2) );  
  9.         System.out.println( Math.floorMod(-27 , 2) );  
  10.         System.out.println( Math.floorMod(50 , 2) );  
  11.         System.out.println( Math.floorMod(51 , 2) );  
  12.     }  
  13. }  

Output

 

Another example

Similarly if we will 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. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println( (8+2) % 8 );  
  9.         System.out.println( (5+8) % 8 );  
  10.         System.out.println( (5-24) % 8 );  
  11.     }  
  12. }  

Output

 

Example: In this 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. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println( Math.floorMod(8+2 , 8) );  
  9.         System.out.println( Math.floorMod(5+8 , 8) );  
  10.         System.out.println( Math.floorMod(5-24 , 8) );  
  11.     }  
  12. }  

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. {  
  6.     public static void main(String args[])throws java.lang.ArithmeticException  
  7.     {  
  8.         System.out.println( Math.nextDown(1000) );  
  9.         System.out.println( Math.nextDown(1.27) );  
  10.     }  
  11. }  
  12.    

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 the 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. {  
  5.     private final StampedLock sl = new StampedLock();  
  6.     private long bal;  
  7.     public StampedLock_Demo(long bal)   
  8.     {  
  9.         this.bal = bal;  
  10.     }  
  11.     public void deposit(long amt)     
  12.     {  
  13.         long stamp = sl.writeLock();  
  14.         try  
  15.         {  
  16.             bal=bal+amt;  
  17.         }  
  18.         finally  
  19.         {  
  20.             sl.unlockWrite(stamp);  
  21.         }  
  22.     }  
  23.     public void withdraw(long amt)    
  24.     {  
  25.         long stamp = sl.writeLock();  
  26.         try   
  27.         {  
  28.             bal =bal-amt;  
  29.         }  
  30.         finally   
  31.         {  
  32.             sl.unlockWrite(stamp);  
  33.         }  
  34.     }  
  35.     public long getBalance()   
  36.     {  
  37.         long stamp = sl.readLock();  
  38.         try  
  39.         {  
  40.             return bal;  
  41.         }  
  42.         finally       
  43.         {  
  44.             sl.unlockRead(stamp);  
  45.         }  
  46.     }  
  47.     public long getBalanceOptimisticRead()    
  48.     {  
  49.         long stamp = sl.tryOptimisticRead();  
  50.         long bal = this.bal;  
  51.         if (!sl.validate(stamp))   
  52.         {  
  53.             stamp = sl.readLock();  
  54.             try  
  55.             {  
  56.                 bal = this.bal;  
  57.             }   
  58.             finally       
  59.             {  
  60.                 sl.unlockRead(stamp);  
  61.             }  
  62.         }  
  63.         return bal;  
  64.     }  
  65. }  

Summary

The summarization of this article is the new features of Java 8 that are very useful for implementation and helps the programmer to get output from the program faster and more precisely. These features will take Java to the next level.

Up Next
    Ebook Download
    View all
    Learn
    View all