Pages

Saturday, November 06, 2010

JDK 7: The wait is over in OpenJDK

I did a presentation last month for our Greenville Java Users Group about the proposed features set for JDK 7. My claim is that they are already here in OpenJDK. Granted there still needs to be some fine tuning, but all of the proposed features will work.
You may need to turn on some JVM parameters to make them work, but they work.


JSR 292: Support for dynamically-typed languages (InvokeDynamic)

The following parameters need to be passed to the JVM:

-XX:+UnlockExperimentalVMOptions \
-XX:+EnableInvokeDynamic


An example of how this is implemented can be found at:

Support for Dynamically Typed Languages in the Java Virtual Machine

JSR TBD: Small language enhancements (Project Coin)

The simplified enhancements to improve developer productivity include:
  • Underscore separators for integral literals (primitive types).
  • "Diamond Operator" for simplified generic instance creation.
  • Strings in switch statements.

JSR 166y: Concurrency and Collections Updates

The best known reference is to ForkJoin operations from JavaOne, but it includes so much more. A great example of a Fork-Join operation can be found at: JSR-166: The Java fork/join Framework.

JSR 203: More new I/O APIs for the Java platform (NIO.2)

In the example at the bottom of the blog, I show the Path class. This includes some additional functionality where we expect "Files" and "Directories (path)" information to be separate.

A really cool new feature is the WatcherService API. This allows us to watch a file, or directory for changes. A great example can be found here: WatchDir.java.

This is just a few examples of the great things that are available in OpenJDK today. So if you want to try out the new features, download a binary distribution for your platform, or better still: download the source and build it yourself.

As promised here is my example code which demonstrates some of the features. You will need to download the ParallelMergeSort from the Fork-Join framework link above to compile it. Also if you are using NetBeans, it has a number of hooks already in place for OpenJDK (JDK 7/8).

Example.java

/*
 *  Copyright 2010 Blue Lotus Software, LLC.
 *  Copyright 2010 John Yeary <jyeary@bluelotussoftware.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/*
 * $Id: Example.java 304 2010-11-06 16:32:05Z jyeary $
 */
package com.bluelotussoftware.example.jdk7;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Arrays;

public class Example {

    public static void main(String... args) {

        Map<String, String> map = new HashMap<>();
        map.put("Hello", "World");
        System.out.println(map.get("Hello"));

        Map<String, Map<String, String>> multimap = new HashMap<>();
        Map<String, String> enclosed = new HashMap<>();
        enclosed.put("name", "value");
        multimap.put("map1", map);
        multimap.put("map2", enclosed);

        System.out.println(multimap.get("map2").get("name"));


        int x = 0b0_1;
        int y = 0b1000_0100_0010_0001;
        int z = 123___456___789___0;

        System.out.println(x);
        System.out.println(y);
        System.out.println(z);

        String testCase = "openJDK";

        switch (testCase) {
            case "yummy": {
                System.out.println("Yummy");
                break;
            }
            case "meloncholy": {
                System.out.println("Sad");
                break;
            }
            case "openJDK": {
                System.out.print("The future of Java...");
            }
            case "jdk7":
                System.out.print(" today.\n");
        }

        File f = new File(System.getProperty("user.home"));
        Path p = f.toPath();
        System.out.println("Current Path: " + p.toString());

        Integer[] assorted = new Integer[5000];
        Random generator = new Random();

        for (int i = 0; i < 5000; i++) {
            assorted[i] = generator.nextInt(1_000_000);
        }
        System.out.println("\n\n\n\n\nunsorted: " + Arrays.toString(assorted) + "\n\n\n\n\n");

        ParallelMergeSort.sort(assorted);

        System.out.println("sorted: " + Arrays.toString(assorted));

    }
}
Enhanced by Zemanta

4 comments :