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()" 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:
- import java.util.*;
- public class Parallel_Sort {
- public static void main(String[] args) {
- List < Integer > list = new ArrayList();
- Random r = new Random();
- for (int x = 0; x < 27061990; x++) {
- list.add(r.nextInt(2706));
- }
- Integer[] array1 = new Integer[list.size()];
- Integer[] array2 = new Integer[list.size()];
- array1 = list.toArray(array1);
- array2 = list.toArray(array2);
- long start = System.currentTimeMillis();
- Arrays.sort(array1);
- long end = System.currentTimeMillis();
- System.out.println("Array Sort Required Time: " + (end - start));
- start = System.currentTimeMillis();
- Arrays.parallelSort(array2);
- end = System.currentTimeMillis();
- System.out.println("Parallel Sort Required Time: " + (end - start));
- }
- }
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
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class oldmath_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(100000 * 100000);
- }
- }
Output
Example
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class math_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(Math.multiplyExact(100000, 100000));
- }
- }
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
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class oldmath_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(-27 % 2);
- System.out.println(-26 % 2);
- System.out.println(50 % 2);
- System.out.println(51 % 2);
- }
- }
Output
Example: But by using "Math.floorMod()" method we can handle this problem.
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class math_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(Math.floorMod(-26, 2));
- System.out.println(Math.floorMod(-27, 2));
- System.out.println(Math.floorMod(50, 2));
- System.out.println(Math.floorMod(51, 2));
- }
- }
Output
Similarly, if we work good (position number) is calculated as (position + adjustment) but, what happens when it gives negative numbers?
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class oldmath_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println((8 + 2) % 8);
- System.out.println((5 + 8) % 8);
- System.out.println((5 - 24) % 8);
- }
- }
Output
Example: Also, by using
the "Math.floorMod()" method, we can handle this problem.
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class math_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(Math.floorMod(8 + 2, 8));
- System.out.println(Math.floorMod(5 + 8, 8));
- System.out.println(Math.floorMod(5 - 24, 8));
- }
- }
Output
Example: This is the example of the "
nextDown()" method.
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class nxtdwn_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(Math.nextDown(1000));
- System.out.println(Math.nextDown(1.27));
- }
- }
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
- import java.util.*;
- import java.util.concurrent.locks.*;
- public class StampedLock_Demo {
- private final StampedLock sl = new StampedLock();
- private long bal;
- public StampedLock_Demo(long bal) {
- this.bal = bal;
- }
- public void deposit(long amt) {
- long stamp = sl.writeLock();
- try {
- bal = bal + amt;
- } finally {
- sl.unlockWrite(stamp);
- }
- }
- public void withdraw(long amt) {
- long stamp = sl.writeLock();
- try {
- bal = bal - amt;
- } finally {
- sl.unlockWrite(stamp);
- }
- }
- public long getBalance() {
- long stamp = sl.readLock();
- try {
- return bal;
- } finally {
- sl.unlockRead(stamp);
- }
- }
- public long getBalanceOptimisticRead() {
- long stamp = sl.tryOptimisticRead();
- long bal = this.bal;
- if (!sl.validate(stamp)) {
- stamp = sl.readLock();
- try {
- bal = this.bal;
- } finally {
- sl.unlockRead(stamp);
- }
- }
- return bal;
- }
- }
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.