sbordet@webtide.com
Java 9/10/11
What’s new and why you should upgrade
@simonebordet
sbordet@webtide.com
Simone Bordet
● @simonebordet
● sbordet@webtide.com
● Java Champion
● Works @ Webtide
○ The company behind Jetty and CometD
sbordet@webtide.com
Java 11
● Java 11 is here!
● “Long Term Support” (LTS) Release
○ Java 8 was the previous LTS Release
○ Java 9 and Java 10 already unmaintained
sbordet@webtide.com
Java 11
OpenJDK 11 Repository
https://hg.openjdk.java.net
Oracle JDK Binary
https://java.oracle.com
OpenJDK Binary
http://jdk.java.net/11
RedHat JDK
Binary
Azul JDK Binary (Zulu)
AdoptOpenJDK Binary
https://adoptopenjdk.net
sbordet@webtide.com
Java 11
● What does LTS really mean?
● During the 6 months of “life” of a Java 11:
○ OpenJDK Binary -> GPL
○ AdoptOpenJDK -> GPL
○ Azul Zulu -> GPL
○ Oracle JDK Binary -> Oracle License
■ MUST PAY Oracle for production use
sbordet@webtide.com
Java 11
● After the 6 months of “life” of a Java 11:
○ Upgrade to Java 12
○ Stay on Java 11
sbordet@webtide.com
● After 6 months, you stay on Java 11
○ Never update -> exposed to vulnerabilities
○ Update Java 11 -> 11.0.x
● Community (RedHat) backports fixes
● Vendors create binary builds
○ AdoptOpenJDK -> GPL
○ Other vendors (Oracle, Azul, RedHat, …) -> PAY
Java 11
sbordet@webtide.com
Java 9
New Features
sbordet@webtide.com
Java 9
● Java 9 introduced the Java Module System
● Along with it, a number of breaking changes
○ Upgrading from 8 to 9/10/11 is NOT simple
○ Many runtime behavior changes
○ Needs very thorough testing
sbordet@webtide.com
Java 9
● Removed tools.jar
○ Attach API, Compiler API, JavaDoc API, etc.
● Removed JavaDB
● Removed endorsed and extension directories
○ $JAVA_HOME/lib/endorsed
○ $JAVA_HOME/jre/lib/ext
sbordet@webtide.com
Java 9
● Class loading implementation changed
○ Different behavior to support modules
ClassLoader sysCL = ClassLoader.getSystemClassLoader();
// Throws ClassCastException now!
URLClassLoader urlCL = (URLClassLoader)sysCL;
sbordet@webtide.com
Java 9
● Loading resources
URL resource = sysCL.getResource("java/lang/String.class");
// Not a file:/ nor a jar:/ URL!
resource = jrt:/java.base/java/lang/String.class
URL resource = sysCL.getResource("/com/foo/bar.properties");
// It’s there, but it won’t find it!
resource = null;
sbordet@webtide.com
Java 9
● New version string scheme
○ 1.8.0_181 -> 9.0.1
○ Broke many Maven Plugins, Jetty, etc.
● JDK 9’s java.lang.Runtime.Version class
○ Cannot parse JDK 8 version string
○ Must implement custom parsing to support both
sbordet@webtide.com
Java 9
● Internal APIs encapsulated
○ Cannot access sun.* or com.sun.* classes
● Finalization
○ sun.misc.Cleaner -> java.lang.ref.Cleaner
○ Object.finalize() -> deprecated
● Unsafe
○ Some sun.misc.Unsafe usage replaced by VarHandle
sbordet@webtide.com
Java 9
● Multi Release jars
com/
acme/
A.class
B.class
META-INF/
versions/
9/
com/
acme/
A.class <- Replaces normal A.class
C.class
sbordet@webtide.com
Java 9
● Variable Handles
○ Expose some Unsafe functionality
class ConcurrentLinkedQueue_BAD {
// BAD, adds indirection
AtomicReference<Node> head;
}
sbordet@webtide.com
Java 9
class ConcurrentLinkedQueue {
private Node head;
private static final VarHandle HEAD;
static {
HEAD = MethodHandles.lookup()
.findVarHandle(ConcurrentLinkedQueue.class,
"head", Node.class);
}
public void m() {
if (HEAD.compareAndSet(...))
...
}
}
}
sbordet@webtide.com
Java 9
● Process APIs
Process p = new ProcessBuilder()
.command("java")
.directory(new File("/tmp"))
.redirectOutput(Redirect.DISCARD)
.start();
ProcessHandle.of(p.pid())
.orElseThrow(IllegalStateException::new)
.onExit()
.thenAccept(h ->
System.err.printf("%d exited%n", h.pid())
);
sbordet@webtide.com
Java 9
● Concurrent APIs Enhancements
○ java.util.concurrent.Flow
■ Identical APIs and semantic of ReactiveStreams
○ CompletableFuture enhancements
■ Common scheduler for timeout functionalities
CompletableFuture.supplyAsync(() -> longJob())
.completeOnTimeout("N/A", 1, TimeUnit.SECONDS)
CompletableFuture.supplyAsync(() -> longJob())
.orTimeout(1, TimeUnit.SECONDS)
sbordet@webtide.com
Java 9
● jshell - Read-Eval-Print-Loop (REPL)
● Pretty powerful!
○ AutoCompletion, Imports, Javadocs
○ History, search
○ Syncs with external editor
○ Function and forward references
○ ...
sbordet@webtide.com
Java 9
● G1 is the default Garbage Collector
● Much improved from 8 and even better in 11
○ Adaptive start of concurrent mark
○ Made internal data structures more concurrent
○ More phases parallelized
○ Reduced contention
○ Reduced memory consumption
sbordet@webtide.com
Java 9
● Unified GC logging
○ Not compatible with previous GC logs
● Every GC logs differently
● Example
○ -
Xlog:gc*,ergo*=trace,ref*=debug:file=log
s/gc.log:time,level,tags
sbordet@webtide.com
Java 9
● JVM options changes
○ 50 options removed - JVM refuses to start
○ 18 options ignored - 12 options deprecated
● Must review your command line!
○ Especially if you had custom GC tuning options
sbordet@webtide.com
Java 10
New Features
sbordet@webtide.com
Java 10
● Local variable type inference (a.k.a. var)
○ http://openjdk.java.net/projects/amber/LVTIstyle.html
// Infers ArrayList<String>
var list = new ArrayList<String>();
// infers Stream<String>
var stream = list.stream();
sbordet@webtide.com
Java 10
● var is not a keyword, it is a reserved type name
○ It’s a valid identifier, unless used in places where the
compiler expects a type name
int var = 13;
sbordet@webtide.com
Java 10
● Good var usage
var message = "warning, too many features";
var anon = new Object() {
int count = 0;
}
anon.count++; // Compiles!
sbordet@webtide.com
Java 10
● Controversial var usage
// What type ?
var result = processor.run();
// IDE cannot help you
var list = new <ctrl+space>
sbordet@webtide.com
Java 10
● Experimental Graal JIT
○ https://www.graalvm.org/
● -XX:+UnlockExperimentalVMOptions -XX:
+UseJVMCICompiler
● Not yet recommended in production
○ Twitter uses it in production, you may too (YMMV)
sbordet@webtide.com
Java 10
● Docker awareness
○ On by default
○ https://blog.docker.com/2018/04/improved-docker-containe
-XX:-UseContainerSupport
-XX:ActiveProcessorCount=n
-XX:MinRAMPercentage=p
-XX:InitialRAMPercentage=p
-XX:MaxRAMPercentage=p
sbordet@webtide.com
Java 11
New Features
sbordet@webtide.com
Java 11
● Two new Garbage Collectors
○ Epsilon GC
○ ZGC
● Epsilon GC
○ No-operation GC
sbordet@webtide.com
● ZGC
○ 5 years of closed source development @ Oracle
○ XX:+UnlockExperimentalVMOptions XX:+UseZGC
○ Single Generation, Region Based
○ Concurrent Marking
○ Concurrent Compaction
○ Very low STW pause times
Java 11
sbordet@webtide.com
Java 11
● Removed methods
○ Thread.stop(Throwable)
○ Thread.destroy()
○ System.runFinalizersOnExit(boolean)
○ Some SecurityManager.check*() methods
sbordet@webtide.com
Java 11
● Removed modules
○ java.activation (JAF)
○ java.corba
○ java.transaction (JTA)
○ java.xml.bind (JAXB)
○ java.xml.ws (JAX-WS)
○ java.xml.ws.annotation (@PostConstruct, ...)
● Replaced by standalone jars
○ https://dzone.com/articles/apis-to-be-removed-from-java-11
sbordet@webtide.com
Java 11
● Java Flight Recorder
○ Open Sourced by Oracle, included in OpenJDK 11
○ java -XX:StartFlightRecording ...
● Java Mission Control
○ Open Sourced by Oracle
○ Downloads at https://jdk.java.net/jmc/
○ JMC 7 scheduled for January 2019
sbordet@webtide.com
Java 11
● TLS 1.3
○ More secure and recent version of TLS
○ Removed vulnerable/weak ciphers
○ Added new ciphers and algorithms
● Using SSLEngine? Verify it works!
sbordet@webtide.com
Java 11
● Launch Single-File Source-Code programs
$ java Hello.java
● Compiled in-memory with Graal
● Teaching
● Java Scripts
sbordet@webtide.com
Java 11
● HTTP client
○ Supports both HTTP/1.1 and HTTP/2
○ Based on the j.u.c.Flow APIs
HttpClient c = HttpClient.newBuilder().build();
HttpRequest r = HttpRequest.newBuilder()
.uri(...).build();
CompletableFuture<String> f = c.sendAsync(r,
BodyHandlers.ofString());
sbordet@webtide.com
Java 11
● Nest Mates
○ Inner (nested) classes introduced in Java 1.1
○ Outer class can access inner class
■ Via compiler tricks - bridge methods
java.lang.UnsupportedOperationException
at NestExample.init(NestExample.java)
at NestExample.access$000(NestExample.java)
at NestExample$Inner.(NestExample.java)
at NestExample.main(NestExample.java)
sbordet@webtide.com
Java 11
● Nest Mates
○ Now specified in the JVM Specification
○ New class file attribute
○ New reflection APIs: Class.getNestMembers(), ...
○ New JVM access controls at runtime
● Do you play with bytecode?
○ Use ASM 7+
sbordet@webtide.com
Conclusions
sbordet@webtide.com
Conclusions
● Upgrade from Java 8 takes time
○ Lots of runtime changes
● Java 8 will be soon unmaintained / unavailable
○ Skip Java 9 and Java 10 - go straight to Java 11
● Lots of good stuff and more coming
○ And coming every 6 months now!
sbordet@webtide.com
Questions?

Java 9/10/11 - What's new and why you should upgrade

  • 1.
    sbordet@webtide.com Java 9/10/11 What’s newand why you should upgrade @simonebordet
  • 2.
    sbordet@webtide.com Simone Bordet ● @simonebordet ●sbordet@webtide.com ● Java Champion ● Works @ Webtide ○ The company behind Jetty and CometD
  • 3.
    sbordet@webtide.com Java 11 ● Java11 is here! ● “Long Term Support” (LTS) Release ○ Java 8 was the previous LTS Release ○ Java 9 and Java 10 already unmaintained
  • 4.
    sbordet@webtide.com Java 11 OpenJDK 11Repository https://hg.openjdk.java.net Oracle JDK Binary https://java.oracle.com OpenJDK Binary http://jdk.java.net/11 RedHat JDK Binary Azul JDK Binary (Zulu) AdoptOpenJDK Binary https://adoptopenjdk.net
  • 5.
    sbordet@webtide.com Java 11 ● Whatdoes LTS really mean? ● During the 6 months of “life” of a Java 11: ○ OpenJDK Binary -> GPL ○ AdoptOpenJDK -> GPL ○ Azul Zulu -> GPL ○ Oracle JDK Binary -> Oracle License ■ MUST PAY Oracle for production use
  • 6.
    sbordet@webtide.com Java 11 ● Afterthe 6 months of “life” of a Java 11: ○ Upgrade to Java 12 ○ Stay on Java 11
  • 7.
    sbordet@webtide.com ● After 6months, you stay on Java 11 ○ Never update -> exposed to vulnerabilities ○ Update Java 11 -> 11.0.x ● Community (RedHat) backports fixes ● Vendors create binary builds ○ AdoptOpenJDK -> GPL ○ Other vendors (Oracle, Azul, RedHat, …) -> PAY Java 11
  • 8.
  • 9.
    sbordet@webtide.com Java 9 ● Java9 introduced the Java Module System ● Along with it, a number of breaking changes ○ Upgrading from 8 to 9/10/11 is NOT simple ○ Many runtime behavior changes ○ Needs very thorough testing
  • 10.
    sbordet@webtide.com Java 9 ● Removedtools.jar ○ Attach API, Compiler API, JavaDoc API, etc. ● Removed JavaDB ● Removed endorsed and extension directories ○ $JAVA_HOME/lib/endorsed ○ $JAVA_HOME/jre/lib/ext
  • 11.
    sbordet@webtide.com Java 9 ● Classloading implementation changed ○ Different behavior to support modules ClassLoader sysCL = ClassLoader.getSystemClassLoader(); // Throws ClassCastException now! URLClassLoader urlCL = (URLClassLoader)sysCL;
  • 12.
    sbordet@webtide.com Java 9 ● Loadingresources URL resource = sysCL.getResource("java/lang/String.class"); // Not a file:/ nor a jar:/ URL! resource = jrt:/java.base/java/lang/String.class URL resource = sysCL.getResource("/com/foo/bar.properties"); // It’s there, but it won’t find it! resource = null;
  • 13.
    sbordet@webtide.com Java 9 ● Newversion string scheme ○ 1.8.0_181 -> 9.0.1 ○ Broke many Maven Plugins, Jetty, etc. ● JDK 9’s java.lang.Runtime.Version class ○ Cannot parse JDK 8 version string ○ Must implement custom parsing to support both
  • 14.
    sbordet@webtide.com Java 9 ● InternalAPIs encapsulated ○ Cannot access sun.* or com.sun.* classes ● Finalization ○ sun.misc.Cleaner -> java.lang.ref.Cleaner ○ Object.finalize() -> deprecated ● Unsafe ○ Some sun.misc.Unsafe usage replaced by VarHandle
  • 15.
    sbordet@webtide.com Java 9 ● MultiRelease jars com/ acme/ A.class B.class META-INF/ versions/ 9/ com/ acme/ A.class <- Replaces normal A.class C.class
  • 16.
    sbordet@webtide.com Java 9 ● VariableHandles ○ Expose some Unsafe functionality class ConcurrentLinkedQueue_BAD { // BAD, adds indirection AtomicReference<Node> head; }
  • 17.
    sbordet@webtide.com Java 9 class ConcurrentLinkedQueue{ private Node head; private static final VarHandle HEAD; static { HEAD = MethodHandles.lookup() .findVarHandle(ConcurrentLinkedQueue.class, "head", Node.class); } public void m() { if (HEAD.compareAndSet(...)) ... } } }
  • 18.
    sbordet@webtide.com Java 9 ● ProcessAPIs Process p = new ProcessBuilder() .command("java") .directory(new File("/tmp")) .redirectOutput(Redirect.DISCARD) .start(); ProcessHandle.of(p.pid()) .orElseThrow(IllegalStateException::new) .onExit() .thenAccept(h -> System.err.printf("%d exited%n", h.pid()) );
  • 19.
    sbordet@webtide.com Java 9 ● ConcurrentAPIs Enhancements ○ java.util.concurrent.Flow ■ Identical APIs and semantic of ReactiveStreams ○ CompletableFuture enhancements ■ Common scheduler for timeout functionalities CompletableFuture.supplyAsync(() -> longJob()) .completeOnTimeout("N/A", 1, TimeUnit.SECONDS) CompletableFuture.supplyAsync(() -> longJob()) .orTimeout(1, TimeUnit.SECONDS)
  • 20.
    sbordet@webtide.com Java 9 ● jshell- Read-Eval-Print-Loop (REPL) ● Pretty powerful! ○ AutoCompletion, Imports, Javadocs ○ History, search ○ Syncs with external editor ○ Function and forward references ○ ...
  • 21.
    sbordet@webtide.com Java 9 ● G1is the default Garbage Collector ● Much improved from 8 and even better in 11 ○ Adaptive start of concurrent mark ○ Made internal data structures more concurrent ○ More phases parallelized ○ Reduced contention ○ Reduced memory consumption
  • 22.
    sbordet@webtide.com Java 9 ● UnifiedGC logging ○ Not compatible with previous GC logs ● Every GC logs differently ● Example ○ - Xlog:gc*,ergo*=trace,ref*=debug:file=log s/gc.log:time,level,tags
  • 23.
    sbordet@webtide.com Java 9 ● JVMoptions changes ○ 50 options removed - JVM refuses to start ○ 18 options ignored - 12 options deprecated ● Must review your command line! ○ Especially if you had custom GC tuning options
  • 24.
  • 25.
    sbordet@webtide.com Java 10 ● Localvariable type inference (a.k.a. var) ○ http://openjdk.java.net/projects/amber/LVTIstyle.html // Infers ArrayList<String> var list = new ArrayList<String>(); // infers Stream<String> var stream = list.stream();
  • 26.
    sbordet@webtide.com Java 10 ● varis not a keyword, it is a reserved type name ○ It’s a valid identifier, unless used in places where the compiler expects a type name int var = 13;
  • 27.
    sbordet@webtide.com Java 10 ● Goodvar usage var message = "warning, too many features"; var anon = new Object() { int count = 0; } anon.count++; // Compiles!
  • 28.
    sbordet@webtide.com Java 10 ● Controversialvar usage // What type ? var result = processor.run(); // IDE cannot help you var list = new <ctrl+space>
  • 29.
    sbordet@webtide.com Java 10 ● ExperimentalGraal JIT ○ https://www.graalvm.org/ ● -XX:+UnlockExperimentalVMOptions -XX: +UseJVMCICompiler ● Not yet recommended in production ○ Twitter uses it in production, you may too (YMMV)
  • 30.
    sbordet@webtide.com Java 10 ● Dockerawareness ○ On by default ○ https://blog.docker.com/2018/04/improved-docker-containe -XX:-UseContainerSupport -XX:ActiveProcessorCount=n -XX:MinRAMPercentage=p -XX:InitialRAMPercentage=p -XX:MaxRAMPercentage=p
  • 31.
  • 32.
    sbordet@webtide.com Java 11 ● Twonew Garbage Collectors ○ Epsilon GC ○ ZGC ● Epsilon GC ○ No-operation GC
  • 33.
    sbordet@webtide.com ● ZGC ○ 5years of closed source development @ Oracle ○ XX:+UnlockExperimentalVMOptions XX:+UseZGC ○ Single Generation, Region Based ○ Concurrent Marking ○ Concurrent Compaction ○ Very low STW pause times Java 11
  • 34.
    sbordet@webtide.com Java 11 ● Removedmethods ○ Thread.stop(Throwable) ○ Thread.destroy() ○ System.runFinalizersOnExit(boolean) ○ Some SecurityManager.check*() methods
  • 35.
    sbordet@webtide.com Java 11 ● Removedmodules ○ java.activation (JAF) ○ java.corba ○ java.transaction (JTA) ○ java.xml.bind (JAXB) ○ java.xml.ws (JAX-WS) ○ java.xml.ws.annotation (@PostConstruct, ...) ● Replaced by standalone jars ○ https://dzone.com/articles/apis-to-be-removed-from-java-11
  • 36.
    sbordet@webtide.com Java 11 ● JavaFlight Recorder ○ Open Sourced by Oracle, included in OpenJDK 11 ○ java -XX:StartFlightRecording ... ● Java Mission Control ○ Open Sourced by Oracle ○ Downloads at https://jdk.java.net/jmc/ ○ JMC 7 scheduled for January 2019
  • 37.
    sbordet@webtide.com Java 11 ● TLS1.3 ○ More secure and recent version of TLS ○ Removed vulnerable/weak ciphers ○ Added new ciphers and algorithms ● Using SSLEngine? Verify it works!
  • 38.
    sbordet@webtide.com Java 11 ● LaunchSingle-File Source-Code programs $ java Hello.java ● Compiled in-memory with Graal ● Teaching ● Java Scripts
  • 39.
    sbordet@webtide.com Java 11 ● HTTPclient ○ Supports both HTTP/1.1 and HTTP/2 ○ Based on the j.u.c.Flow APIs HttpClient c = HttpClient.newBuilder().build(); HttpRequest r = HttpRequest.newBuilder() .uri(...).build(); CompletableFuture<String> f = c.sendAsync(r, BodyHandlers.ofString());
  • 40.
    sbordet@webtide.com Java 11 ● NestMates ○ Inner (nested) classes introduced in Java 1.1 ○ Outer class can access inner class ■ Via compiler tricks - bridge methods java.lang.UnsupportedOperationException at NestExample.init(NestExample.java) at NestExample.access$000(NestExample.java) at NestExample$Inner.(NestExample.java) at NestExample.main(NestExample.java)
  • 41.
    sbordet@webtide.com Java 11 ● NestMates ○ Now specified in the JVM Specification ○ New class file attribute ○ New reflection APIs: Class.getNestMembers(), ... ○ New JVM access controls at runtime ● Do you play with bytecode? ○ Use ASM 7+
  • 42.
  • 43.
    sbordet@webtide.com Conclusions ● Upgrade fromJava 8 takes time ○ Lots of runtime changes ● Java 8 will be soon unmaintained / unavailable ○ Skip Java 9 and Java 10 - go straight to Java 11 ● Lots of good stuff and more coming ○ And coming every 6 months now!
  • 44.