Top 6 Java 8 features you can start using now -- part 2
Extends Features 3 - 6 in Top 6 Java 8 features you can start using now -- part 1
#4 Java 8 corrects its lack of good support for JavaScript by introducing a new JVM JavaScript engine named "Nashorn". Together with a new command line program called "jjs" it is trivial to write applications in Javascript which in turn can utilize any JVM based library.
C:\>C:\TOOLS\java\jdk1.8.0_ea\bin\jjs c:\temp\HelloWorld.js hello world
The JavaScript file HelloWorld.js has the line
java.lang.System.out.println("hello world");
#5 Parallel processing. In pre Java 8, the call to Arrays.sort( ) uses the merge sort algorithm sequentially. Java 8, introduces a new API for sorting the array in parallel with Arrays.parallelSort( ). Arrays.parallelSort( ) uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool. Fork/Join implements a work stealing algorithm where an idle thread can steal tasks queued up in another thread.
package com.java8.examples; import java.util.Arrays; public class ParallelSortingTest { private static final String[] languages = {"Java", "C", "PHP", "C#", "VB", "Scala"}; private static final String[] languages2 = {"Sql", "Unix", "Regex", "XML", "Json", "Groovy"}; public static void main(String[] args) { Arrays.sort(languages); System.out.println(Arrays.asList(languages)); Arrays.parallelSort(languages2); System.out.println(Arrays.asList(languages2)); } }
Output:
[C, C#, Java, PHP, Scala, VB] [Groovy, Json, Regex, Sql, Unix, XML]
Not only sorting, but also other operations like filtering and mapping on a collection like list or map can be carried out in parallel by the framework. Here is an example.
package com.java8.examples; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class ParallelismTest { private static final String[] languages = {"Java", "C", "PHP", "C#", "VB", "Scala"}; public static void main(String[] args) { List<String> languageList = Arrays.asList(languages); // extract length of each String using lambda expression Stream lengths = languageList.stream().map(name -> name.length()); System.out.println("Processed sequentially: " + Arrays.asList(lengths.toArray())); Stream lengthsParallel = languageList.parallelStream().map(name -> name.length()); System.out.println("Processed in parallel: " + Arrays.asList(lengthsParallel.toArray())); } }
Output:
Processed sequentially: [4, 1, 3, 2, 2, 5] Processed in parallel: [4, 1, 3, 2, 2, 5]
#6 The java.util.concurrent.atomic package has been expanded to include four new classes that allow for concurrent, scalable, and updatable variables. LongAccumulator, LongAdder, DoubleAccumulator and DoubleAdder can be used to sum a value across multiple threads, and is generally preferable to the AtomicLong class since it’s considerably faster.
package com.java8.examples; import java.util.concurrent.atomic.LongAdder; public class LongAdderTest { private static LongAdder counter = new LongAdder(); public static void main(String[] args) { //the main thread spawns 2 threads using the lambda expressions //and increments the counter (i.e. LongAdder) new Thread(() -> { counter.increment(); System.out.println(Thread.currentThread().getName() + " count -- " + counter.longValue()); }).start(); Thread t2 = new Thread(() -> { counter.increment(); System.out.println(Thread.currentThread().getName() + " count -- " + counter.longValue()); }); t2.start(); } }
Output:
Thread-0 count -- 1 Thread-1 count -- 2
Note: Prior to Java 8, the Java HotSpot VM had a memory space known as PermGen space for storing class metadata. This often lead to java.lang.OutOfMemoryError due to susing up all the PermGen space. In Java 8, the HotSpot VM will no longer have PermGen space, and will instead allocate native memory, called Metaspace to store class metadata, which is similar to the way the JRockit and IBM JVMs operate.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home