Java 7 Whats New(), Whats Next() from Oredev

84
7 What’s New() What’s Next() ©Copyright 2011

description

Finally Java SE 7 is GA and you can start using it. This talk will cover the most important new features of the language and the virtual machine. It will also cover some features that did not make it in to the SE 7 release. Finally we will discuss current state of Java as an ecosystem and my analysis and hopes for the future.

Transcript of Java 7 Whats New(), Whats Next() from Oredev

Page 1: Java 7 Whats New(), Whats Next() from Oredev

7What’s New()What’s Next()

©Copyright 2011

Page 2: Java 7 Whats New(), Whats Next() from Oredev

Now()

11:11 11/11/11

Page 3: Java 7 Whats New(), Whats Next() from Oredev

DisclaimerI am just an ordinary developer

I don’t work for Oracle

Page 4: Java 7 Whats New(), Whats Next() from Oredev

Mattias Karlsson

Twitter: @matkarwww.Linkedin/in/mattiask

Avega Group

Page 5: Java 7 Whats New(), Whats Next() from Oredev

www.jforum.se

JavaforumJavaforum

Page 6: Java 7 Whats New(), Whats Next() from Oredev
Page 7: Java 7 Whats New(), Whats Next() from Oredev

7

Page 8: Java 7 Whats New(), Whats Next() from Oredev

Biggest news?

Page 9: Java 7 Whats New(), Whats Next() from Oredev

Java 7 is GA

Page 10: Java 7 Whats New(), Whats Next() from Oredev

HistoryJDK/YEAR Comment

Feb 1997JDK 1.1

Retooling of the AWT event model, inner classes added, JavaBeans and JDBC.

Dec 1998J2SE 1.2 (Playground)

Reflection, a Collections framework, Java Swing API Java Plug-in, JIT compiler.

May 2000J2SE 1.3 (Kestrel)

HotSpot JVM, JavaSound, JNDI and JPDA

Feb 2002Java SE 1.4 (Merlin)

RegEx, exception chaining, XML parser and XSLT (JAXP), Java Web Start

Sep 2004Java SE 5 (Tiger)

for-each loop, generics, autoboxing and var-args

Dec 2006Java SE 6 (Mustang)

scripting languages with the JVM, VB lang support. Annotations (JSR 269)

Page 11: Java 7 Whats New(), Whats Next() from Oredev

Java SE 7

Started in Aug 2006

Page 12: Java 7 Whats New(), Whats Next() from Oredev

Why was 7 delayed• Open Sourcing JDK• Conflicts Blocking JCP• Sun Finance• JavaFX• Oracle Deal

Page 13: Java 7 Whats New(), Whats Next() from Oredev

Trouble in paradise

• James Gosling leaving Oracle

• Oracle vs Google

• Apache leaving JCP

• Doug Lea, Bob Lee leaving JCP

Page 14: Java 7 Whats New(), Whats Next() from Oredev
Page 15: Java 7 Whats New(), Whats Next() from Oredev

”It‘s been clear for some time that the most recent JDK 7 development schedule is, to put it mildly, Unrealistic”

(Mark Reinhold, 8 Sep 2010)

Java 7JDK 7

Plan A,Late 2012

Page 16: Java 7 Whats New(), Whats Next() from Oredev

Plan B (Mark R)

8 Sep 2010 ”Re-thinking JDK 7”

20 Sep 2010 ”It’s time for…Plan B”

10 Oct 2010 ”Plan B: The details”

Page 17: Java 7 Whats New(), Whats Next() from Oredev

Plan B

JDK 8JDK 7JDK 7

Late 2012

Mid 2011 Late 2012

Page 18: Java 7 Whats New(), Whats Next() from Oredev

7JSR 336

JDK 7

New garbage collector G1

Library changes

Enhancements to the JVM

Language changes (Coin)

Concurrency and collections

NIO.2

Plan B

Page 19: Java 7 Whats New(), Whats Next() from Oredev

JSR 336 approval

Page 20: Java 7 Whats New(), Whats Next() from Oredev

Performance

Page 21: Java 7 Whats New(), Whats Next() from Oredev

• Still considered experimental

• Leads to much smaller pause times

• Parallelism and Concurrency

• Generational

• Compaction

• Predictability (over CMS)

G1 Garbage collector

Page 22: Java 7 Whats New(), Whats Next() from Oredev

Using G1• Replace CMS (Concurrent mark sweep) GC

• Enable with:

-XX:+UnlockExperimentalVMOptions –XX:+UseG1GC

• GC Paus time goal:

-XX:MaxGCPauseMillis=50

www.oracle.com/technetwork/java/javase/tech/g1-intro-jsp-135488.html

Page 23: Java 7 Whats New(), Whats Next() from Oredev

Benchmarks• Test 1. Add 5 Million String values (each calculated

with some complex Math arithmetic)

• Test 2. ArrayList <String> with 5 Million insertions (with values from Test1)

• Test 3. HashMap <String, Integer> with 5 million keys, values. Each key, value pair is being calculated via concurrent thread)

• Test 4. Printing 5 million items of ArrayList

http://geeknizer.com/java-7-whats-new-performance-benchmark-1-5-1-6-1-7/

Page 24: Java 7 Whats New(), Whats Next() from Oredev

Performance

http://geeknizer.com/java-7-whats-new-performance-benchmark-1-5-1-6-1-7/

Page 25: Java 7 Whats New(), Whats Next() from Oredev

Performance

Java 6 18% faster then Java 5Java 7 46% faster then Java 6

http://geeknizer.com/java-7-whats-new-performance-benchmark-1-5-1-6-1-7/

Page 26: Java 7 Whats New(), Whats Next() from Oredev

Concurrency Matters Again

1970 1980 1990 2000 2010

110

1001,000

10,000

1,000,000100,000

10,000000

Clock (MHz)

Transistors

Page 27: Java 7 Whats New(), Whats Next() from Oredev

Fork/JoinJava 7 concurrency

JSR 166y

Page 28: Java 7 Whats New(), Whats Next() from Oredev

JSR 166y Fork/Join

• similar to MapReduce• useful for a certain class of

problems

Page 29: Java 7 Whats New(), Whats Next() from Oredev

Result solve(Problem problem) {

if (problem is small enough) directly solve problem else { split problem into independent parts fork new subtasks to solve each part join all subtasks compose result from subresults }}

JSR 166y Fork/Join

Page 30: Java 7 Whats New(), Whats Next() from Oredev

JSR 166y Fork/Join

• Create one ForkJoinPool• Wrap code in ForkJoinTask• Subclass RecursiveAction or RecursiveTask<E>• Need to override compute() method

download.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html

Page 31: Java 7 Whats New(), Whats Next() from Oredev

class Sum extends RecursiveTask<Long> { static final int SEQUENTIAL_THRESHOLD = 1000; Sum(int[] arr, int lo, int hi) { array = arr; low = lo; high = hi; } protected Long compute() { if(high - low <= SEQUENTIAL_THRESHOLD) { long sum = 0; for(int i=low; i < high; ++i)

sum += array[i];return sum;

} else { int mid = low + (high - low) / 2; Sum left = new Sum(array, low, mid); Sum right = new Sum(array, mid, high);

invokeAll(left, right);return right.join() + left.join();

} }}

Page 32: Java 7 Whats New(), Whats Next() from Oredev

DEMOFork/Join Java 7

JSR 166y

Page 33: Java 7 Whats New(), Whats Next() from Oredev

Still too complicated?

mutable state, locks and visible by default is outdatedjava.util.concurrent helps, but can only go so far

move from explicit to implicit parallelism

Page 34: Java 7 Whats New(), Whats Next() from Oredev

Other JVM Languages(free to innovate)

Scala/Akka• Powerful actors model• Parallel Collections

Clojure• Immutable by default• Thread-isolation by default• STM subsystem• Multiple concurrency models

Page 35: Java 7 Whats New(), Whats Next() from Oredev

CoinJSR 334

Page 36: Java 7 Whats New(), Whats Next() from Oredev

Project CoinA set of small language

changes should be added to JDK 7 (OpenJDK)

Page 37: Java 7 Whats New(), Whats Next() from Oredev

Strings in switch

Add the ability to switch on string values analogous to the existing ability to switch on values of the primitive types.

Page 38: Java 7 Whats New(), Whats Next() from Oredev

String s = ...switch(s) { case "quux": processQuux(s); // fall-through case "bar": processFooOrBar(s); break; case "baz": processBaz(s); default: processDefault(s); break;}

Strings in switch

Page 39: Java 7 Whats New(), Whats Next() from Oredev

Improved Exception Handling

• Catching multiple exception types

• A single catch clause can now catch more than one exception types

• Improved checking for re-thrown exceptions

Page 40: Java 7 Whats New(), Whats Next() from Oredev

try { doWork(file);} catch (IOException|SQLException ex) { throw ex;*}

Improved Exception Handling

try { doWork(file);} catch (final Throwable t) { throw t; // ExceptionA or ExceptionB}

Page 41: Java 7 Whats New(), Whats Next() from Oredev

public static void main(String... args) { // THE OLD WAY int oldBillion = 1000000000;

// THE NEW WAY int newBillion = 1_000_000_000;}

Underscore in numbers

Page 42: Java 7 Whats New(), Whats Next() from Oredev

Automatic Resource Management• A *resource* is as an object that used to be

closed manually

• java.io.InputStream

• OutputStream

• Reader, Writer

• java.sql.Connection, Statement, ResultSet

• When the statement completes, whether normally or abruptly, all of its resources are closed automatically.

Page 43: Java 7 Whats New(), Whats Next() from Oredev

Pre Java 7public void copy(String src, String dest) throws IOEx{ InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); try { byte[] buf = new byte[8 * 1024]; int n; while ((n = in.read(buf)) >= 0) out.write(buf, 0, n); } finally { in.close(); out.close(); }}

Page 44: Java 7 Whats New(), Whats Next() from Oredev

Pre Java 7public void copy(String src, String dest) throws IOEx{ InputStream in = new FileInputStream(src); try { OutputStream out = new FileOutputStream(dest); try { byte[] buf = new byte[8 * 1024]; int n; while ((n = in.read(buf)) >= 0) out.write(buf, 0, n); } finally { out.close(); } } finally { in.close(); }}

Page 45: Java 7 Whats New(), Whats Next() from Oredev

public void copy(String src, String dest) throws IOEx{

try ( InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)){ byte[] buf = new byte[8 * 1024]; int n; while ((n = in.read(buf)) >= 0) out.write(buf, 0, n); } //in and out closes automagically}

Automatic Resource Management

Page 46: Java 7 Whats New(), Whats Next() from Oredev

Improved Type Inference for Generic Instance<>

Page 47: Java 7 Whats New(), Whats Next() from Oredev

Improved Type Inference for Generic Instance

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

Map<String, List<String>> anagrams = new HashMap<>();

Page 48: Java 7 Whats New(), Whats Next() from Oredev

NIO.2Network and File System

JSR 203

Page 49: Java 7 Whats New(), Whats Next() from Oredev

NIO.2Network and File System – JSR 203

• java.io.File

• java.nio.file.Path

• java.nio.file.FileSystem

• Other Usefull Methods

Page 50: Java 7 Whats New(), Whats Next() from Oredev

URL url = new URL("http://www.blog.se/?page_id=1");try ( FileOutputStream fos = new FileOutputStream(new File("output.txt")); InputStream is = url.openStream() ){ byte[] buf = new byte[4096]; int len; while ((len = is.read(buf)) > 0){ fos.write(buf, 0, len); }} catch (IOException e){ e.printStackTrace();}

Pre Java 7

Page 51: Java 7 Whats New(), Whats Next() from Oredev

URL url = new URL("http://www.blog.se/?page_id=1");try (InputStream in = url.openStream()) { Files.copy(in, Paths.get("output.txt"));}catch(IOException ex){ ex.printStackTrace();}

NIO.2Network and File System – JSR 203

Page 52: Java 7 Whats New(), Whats Next() from Oredev

Da Vinci Machine Project

Page 53: Java 7 Whats New(), Whats Next() from Oredev

Da Vinci Machine Project• Support for dynamically typed

languages

• Python, Ruby, Perl, Javascript, Groovy...

• Implementations will be more efficient

• JVM level support

• for such languages and their implementers

Page 54: Java 7 Whats New(), Whats Next() from Oredev

Charles Nutter

co-leads Jruby projectEngine Yard

Page 55: Java 7 Whats New(), Whats Next() from Oredev

JSR-292

invokedynamic

Method Handles

WTF?

Page 56: Java 7 Whats New(), Whats Next() from Oredev

Without invokedynamic

Four existing forms of method dispatch

virtual, interface, special, super

JVM decides how they work

What if you don’t fit?

Page 57: Java 7 Whats New(), Whats Next() from Oredev

invokedynamic

User-defined dispatch

Name, signature as usual

Additional bootstrap method handle

Bootstrap called first time to wire it up

JVM then optimizes like “real Java”

Page 58: Java 7 Whats New(), Whats Next() from Oredev

Method Handles

Super-awesome function pointers

Faster than reflection

Curry-able

Still talking directly to JVM

Used by invokedynamic bootstrapping

Page 59: Java 7 Whats New(), Whats Next() from Oredev

invokedynamic

invokedynamic

bytecode

bootstrap method

JVM

1st time

target method

2nd time

Page 60: Java 7 Whats New(), Whats Next() from Oredev

Example

public static void print(String s) { System.out.println(s);}

Page 61: Java 7 Whats New(), Whats Next() from Oredev

Example

invokedynamicbytecode

name andsignature

bootstraphandle

public static void main(java.lang.String[]); Code: 0: ldc #9 // String Hello, invokedynamic! 2: invokedynamic #18, 0 // InvokeDynamic #0:print:(Ljava/lang/String;)V 7: return

Page 62: Java 7 Whats New(), Whats Next() from Oredev

Example

public static CallSite bootstrap(Lookup lookup, String name, MethodType signature{ MethodHandle handle = lookup.findStatic(this_class, name, signature); CallSite site = new ConstantCallSite(handle); return site;}

Page 63: Java 7 Whats New(), Whats Next() from Oredev
Page 64: Java 7 Whats New(), Whats Next() from Oredev

JVM languages to benefit from invokedynamic

• These gains will vary:• JRuby ?

• Clojure ?

• Increasing its performance

• Big potential wins

Page 65: Java 7 Whats New(), Whats Next() from Oredev

Other stuff in• TLS 1.2• Elliptic-curve cryptography (ECC)• JDBC 4.1• Binary literals• Better way to handle unsigned literals • Unicode 6.0• Locale enhancement• Support for IETF BCP 47 and UTR 35

Page 66: Java 7 Whats New(), Whats Next() from Oredev

Other stuff in

• New platform APIs for 6u10 graphics features

• Swing Nimbus look-and-feel• Swing JLayer component • Updated XML stack

Page 67: Java 7 Whats New(), Whats Next() from Oredev

Runtimes

Page 68: Java 7 Whats New(), Whats Next() from Oredev

Java 7 Runtimes• Windows x86

• Server 2008, Server 2008 R2, 7 & 8• Windows Vista, XP

• Linux x86• Oracle Linux 5.5+, 6.x• Red Hat Enterprise Linux 5.5+, 6.x• SuSE Linux Enterprise Server 10.x,

11.x• Ubuntu Linux 10.04 LTS, 11.04

• Solaris x86/SPARC• Solaris 10.9+, 11.x

Page 69: Java 7 Whats New(), Whats Next() from Oredev

Other runtimes”The IBM SDK for Java V7 is now available for the following platforms:” - 20 Sep 2011

• AIX

• Linux

• z/OS

Page 70: Java 7 Whats New(), Whats Next() from Oredev

?http://openjdk.java.net/projects/macosx-port/

Page 71: Java 7 Whats New(), Whats Next() from Oredev

Java 7 Tool Support

NetBeans 7Eclipse 3.7 3.8 4.1 4.2

IntelliJ 10.5

Page 72: Java 7 Whats New(), Whats Next() from Oredev

Whats.Next()

Page 73: Java 7 Whats New(), Whats Next() from Oredev

JRockitHotSpot HotRockit

JVM Convergence

Project “HotRockit”

Page 74: Java 7 Whats New(), Whats Next() from Oredev

Plan B

JDK 8

8JSR 337

Modularization (Jigsaw)

Language and VM Support

Platform Modularization

Project Lambda (JSR 335)

Lambda Expressions for Java

Type Annotations (JSR 308)

Project Coin part II

Date and Time API (JSR 310)

Page 75: Java 7 Whats New(), Whats Next() from Oredev

Plan B++

JDK 8

8JSR 337

Project Nashorn

New implementation of JavaScript

Built from the ground up

Leverage JVM and invoke dynamic

Java FX 3.0 Open Sourced Bundled with Java 8

Page 76: Java 7 Whats New(), Whats Next() from Oredev

Schedule

JDK 8

8JSR 337

May 2011: Expert Group formation

Sep 2011: Early Draft Review

Feb 2012: Public Review

May 2012: Proposed Final Draft

Oct 2012: Final Release

Page 77: Java 7 Whats New(), Whats Next() from Oredev

Schedule

JDK 8

8JSR 337

May 2011: Expert Group formation

Sep 2011: Early Draft Review

Feb 2012: Public Review

May 2012: Proposed Final Draft

Summer 2013: Final Release

Page 78: Java 7 Whats New(), Whats Next() from Oredev

JDK 8

8JSR 337

JDK 8, b10Developer

Preview Now Available!

jdk8.java.net

Schedule

Page 79: Java 7 Whats New(), Whats Next() from Oredev

JCP.next• Using the process to improve the process• JSR 348

• Transparency• Participation• Agility• Governance

• A second JSR will be filed soon• JavaSE and JavaME EC - merged

Page 80: Java 7 Whats New(), Whats Next() from Oredev

JCP, new members

Azul System

Twitter

Page 81: Java 7 Whats New(), Whats Next() from Oredev

We’re moving again

• The JCP Dead Lock Needed to be Broken

• OpenJDK is liberated (GPL)

• JUG attendances are growing

• Oracle marketing estimates 9-10 million devs

• Dozens of new JVM languages

• A Vibrant Java Ecosystem

Page 82: Java 7 Whats New(), Whats Next() from Oredev

Community

Page 83: Java 7 Whats New(), Whats Next() from Oredev

Summary• Java 7 is GA

• Java 8 is on its way

• The JVM Rocks

• JCP.next

• The community is very much alive!

Page 84: Java 7 Whats New(), Whats Next() from Oredev

Mattias Karlssonwww.linkedin.com/in/mattiasktwitter: @[email protected]

©Copyright 2011

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names appearing on the slides may be trademarks of their respective owners.

The development, release, and timing of any features or functionality described for Oracle‘s products remains at the sole discretion of Oracle and JCP