Java >= 9
Modules, Containers and New Things You Can Do In Private
Outline
• Java 9
• Java 10
• Java 11
• Versioning
• LTS and Support
Java 9
ProcessHandle / ProcessHandle.Info
ProcessHandle self = ProcessHandle.current();
long pid = self.getPid();
ProcessHandle.Info info = self.info();
Optional<String[]> args = info.arguments();
Optional<String> cmd = info.commandLine();
Optional<Instant> started = info.startInstant();
Optional<Duration> cpu = info.totalCpuDuration();
ProcessHandle children()
Stream<ProcessHandle> children = ProcessHandle.current().children();
children.forEach(handle -> {
assertTrue("Could not kill process " + handle.getPid(), handle.destroy());
});
Interface Private Methods
interface IntrovertInterface {
private static String staticPrivate() { return ”personal”; }
private String instancePrivate() { return ”library”; }
default void invoke() {
String result = staticPrivate();
result += ” ”.concat(instancePrivate());
System.out.println(result);
}
}
Interfaces Now Allow
1. Constant variables
2. Abstract methods
3. Default methods
4. Static methods
5. Private methods
6. Private static methods
Updated try-with-resources
Now manage final or effectively final variables
Before:
try (PrintWriter writer = new PrintWriter(new File("test.txt"))) {
writer.println("Hello World”);
}
After:
PrintWriter writer = new PrintWriter(new File("test.txt"));
try(writer){
writer.println(“…”);
}
Diamond Operator Extension
Diamond Operator <> extended to Anonymous Inner Classes
WitnessClass<Integer> fc = new WitnessClass<>(1) {
// anonymous inner class impl
};
WitnessClass<? extends Integer> fc0 = new WitnessClass<>(1000) {
// anonymous inner class impl
};
WitnessClass<?> fc1 = new WitnessClass<>(“1”) {
// anonymous inner class impl
};
New APIs
• java.util.Set.of()
• Set<String> keys = Set.of("key1", "key2", "key3");
• java.util.Optional.stream()
• List<String> filteredList = listOfOptionals.stream()
.flatMap(Optional::stream)
.collect(Collectors.toList());
• java.awt.image.MultiResolutionImage
Tooling Changes
• JShell REPL
• JCMD updates
• jhat removed
• Multi-release jar files
• JVM hprof agent removed
• Javadoc HTML5 + Search
Other Enhancements
• Publish-Subscribe for Reactive Streams (java.util.concurrent.Flow)
• Unified JVM Logging
• Bonus Incubating Feature – new HTTP Client
• Stack Walking API
• Variable Handles
• In java.lang.invoke - VarHandle and MethodHandles. It provides equivalents
of java.util.concurrent.atomic and sun.misc.Unsafe operations on object fields
and array elements with similar performance. In Java 9 Modular system
sun.misc.Unsafe will not be possible from application code.
Modules
• One of the biggest changes to Java since Generics
• Even Javadoc got an overhaul because of Modules.
Note:
There are literally books on this topic.
• https://javamodularity.com/
• https://www.manning.com/books/the-java-module-system
The Goals of JSR-376 / Project Jigsaw
• Reliable configuration
• Strong encapsulation
• Scalable platform
• Greater platform integrity
• Improved performance
module-info.java
• Included in jar file at the root of the package hierarchy.
module <module_name> {
}
Keywords - requires
• requires <module_name> - allows access to public types exported
in the target module
• requires transitive <module_name> - any modules depending
on this module also depend on the transitive module
Keywords - exports
• exports <package_name> - makes public members in a package
available to be required.
• Qualified export – limits the module to which a package is made
available.
exports <package_name> to <module_name>
Keywords - uses / provides
• Service Provider Interface keywords.
• uses <class_name> - makes the current module a consumer of the
specified service class.
• provides <class_name> with <class_name_impl> -
identifies/registers a class which implements a service class
Keywords - opens / open
• opens <package_name> - allows reflection based access to all
members of a specified package.
• Qualified open – limits the module to which a package is made
opened.
opens <package_name> to <module_name>
• Open module - defines a module as previously shown but allows
runtime access to all packages via reflection
open module <module_name> …
Example Module
$ java --describe-module java.sql
java.sql@9.0.4
exports java.sql
exports javax.sql
exports javax.transaction.xa
requires java.logging transitive
requires java.base mandated
requires java.xml transitive
uses java.sql.Driver
The java.se module
jlink
• Make your own JRE
• Includes only the necessary components to run your application
• Default JRE size is 200+ MB
• Applications built with jlink can be much smaller
Some Caveats
• This is a (woefully incomplete) overview.
• Tooling Support is still being developed
• Gradle for example does not currently support building or jlinking modules
without the use of an incubating plugin.
• Existing libraries may require rework – e.g, repackaged jars.
• Module system is easily circumvented by using CLASSPATH instead of
MODULEPATH
Java 10
Local Variable Type Inference
• var keyword (but it’s not JavaScript)
• Before Java 7
• Map<String, List<String>> tagged = new HashMap<String,List<String>>();
• Java 7:
• Map<User, List<String>> tagged = new HashMap<>();
• Java 10
• var tagged = new HashMap<User, List<String>>();
Use Cases
• Does exactly what it says it does.
• Local variables only, in contexts where type can be inferred.
• You can’t do this:
• var x = null;
• var y;
• But you can do this:
• var list = new ArrayList<>();
• Inferred as ArrayList<Object> which is probably not what you want.
Better Containerized Behavior
• Memory
• Before - JVM defaults to 1/4th physical memory of the server.
• After - JVM reads memory limits from container cgroup.
• Similar issue with CPUs.
• JVMs assume they have greater access to resources.
• Especially if code uses Runtime.availableProcessors()
• Attach Protocol
• Before - not possible to attach from host to container JVM.
• After - container JVM finds its PID in the root namespace and uses that to
watch for JVM attachment.
Under the Hood
• Garbage Collector Interface (improve the modularity of various GC
implementations) (JEP 304)
• Parallel full GC for G1 (improve worst-case latencies with the default
collector) (JEP 307)
• Experimental Graal Java-based JIT compiler (can be enabled on Linux) (JEP
317)
• Remove the Native Header generation tool (javah) (JEP 313)
Under the Hood (cont)
• Root Certificates and Certification Authority (JEP 319)
• Thread-local Handshakes (Makes it both possible and cheap to stop
individual threads and not just all threads or none)(JEP 312)
• Heap Allocation on Alternative Memory Devices (JEP 316)
• Additional Unicode Language-Tag Extensions (JEP 314)
Java 11
What’s In Java 11
• Local Variable Syntax for Lambda Parameters (JEP 323)
• Allow var to be used to declare the formal parameters of an implicitly typed
lambda expression.
• Instead of
(var x, var y) -> x.process(y)
• We’ll be able to simply write:
(x, y) -> x.process(y)
What’s In Java 11
• Epsilon: A No-op Garbage Collector (JEP 318)
• Dynamic Class File Constants (JEP 309)
• "seek[s] to reduce the cost and disruption of creating new forms of
materializable class-file constants, which in turn offers language designers and
compiler implementors broader options for expressivity and performance."
What’s Not in Java 11
• Java EE and CORBA Modules
Here There Be Ranting …
Versioning
JDK Release Cadence
• Release every six months
• LTS Release every 3 years
• 8 is an LTS release
• 11 is an LTS release
• 17 will be the next LTS release in 2021
Version Identifiers
• Java 8 is 1.8
• Java 9 is 9.0
• Java 10 is 18.3*
• Java 11 is 18.9*
*Proposed versioning scheme (year.month)
Long Term Support
For varying definitions of Support
Public Updates
• Support for a release ends with the next release.
• Java 8 - No updates after Jan. 2019
• Java 9 - No updates after Mar. 2018 (Java 10 release date)
• Java 10 - No updates after Sept. 2018 (Java 11 release date)
• Java 11 - TBA
• should in theory end with the next LTS version 17 in 2021
• but only if you’re paying for support, otherwise, no updates after Java 12
Other Support Options
• https://www.azul.com/products/zulu-and-zulu-enterprise/
• https://adoptopenjdk.net/
Questions
Resources
• https://docs.oracle.com/javase/9/whatsnew/toc.htm
• https://www.pluralsight.com/blog/software-development/java-9-new-features
• https://blog.takipi.com/5-features-in-java-9-that-will-change-how-you-develop-software-and-2-that-wont/
• http://www.baeldung.com/new-java-9
• https://www.opsian.com/blog/java-on-docker/
• http://www.oracle.com/technetwork/java/javase/10-relnote-issues-4108729.html
• https://dzone.com/articles/java-10-new-features-and-enhancements
• https://blog.takipi.com/java-11-will-include-more-than-just-features/
• https://dzone.com/articles/an-early-look-at-features-targeted-for-java-11
• https://medium.com/@afinlay/java-11-sneak-peek-local-variable-type-inference-var-extended-to-lambda-expression-
parameters-e31e3338f1fe
• http://www.oracle.com/technetwork/java/javase/eol-135779.html
• https://jaxenter.com/end-life-comes-early-jdk-8-140824.html