Column catalog

  1. Time to upgrade Java11 -01- JDK11 advantages and JDK options
  2. Time to upgrade Java11 -02- Upgrade JDK11
  3. Time to upgrade Java11 -03 VIRTUAL machine Jvm parameter Settings
  4. Http2 Clear Text (H2C)
  5. Time to update java11 – obstacles and issues with h2c communication in 05 microservices

Java8 commercial charges

As of January 2019, the Oracle JDK has begun to charge for commercial releases of Java SE 8, specifically 8U201/202. If you’re developing Java functionality for business use, the latest version you can use for free is 8U201/202 if you don’t want to pay for it yet. Of course, individual clients or developers can try all versions of the Oracle JDK for free.

Java11 performance improvements

There was a 16% improvement just by switching to Java 11, an improvement likely due to the introduction of JEP 307: Parallel Full GC for G1 in Java 10.

See how much faster Is Java 11 than 8? Look at this benchmark

Overview of changes from Java 8 to Java 11

Note: We won’t cover all the features here, but only some of the changes that developers are most concerned about.

Compact string

Since Java 9 String data has been loaded with compact strings from char[] to byte[], which in many cases only contain latin-1 characters, which save half the memory.

Enhance the API

1. String enhancement @since 11

// Determine if the string is blank
"".isBlank(); // true
// Remove the leading and trailing Spaces
" Hello Java11 ".strip(); // "Hello Java11"
// remove trailing Spaces
" Hello Java11 ".stripTrailing(); // " Hello Java11"
// remove the header space
" Hello Java11 ".stripLeading(); // "Hello Java11 "
// Copy the string
"Java11".repeat(3); // "Java11Java11Java11"
// Line count
"A\nB\nC".lines().count(); / / 3
Copy the code

2. Collection enhancement

Since Java 9, the JDK has added of and copyOf methods for collections (List, Set, Map). They are used to create immutable collections.

  • of() @since 9
  • copyOf() @since 10

Example 1:

var list = List.of("Java"."Python"."C"); // Immutable set
var copy = List.copyOf(list); //copyOf checks if it is an immutable collection type and returns it directly
System.out.println(list == copy); // true
var list = new ArrayList<String>(); // return the normal collection
var copy = List.copyOf(list); // return an immutable set
System.out.println(list == copy); // false
Copy the code

Example 2:

var set = Set.of("Java"."Python"."C");
var copy = Set.copyOf(set);
System.out.println(set == copy); // true
var set1 = new HashSet<String>();
var copy1 = List.copyOf(set1);
System.out.println(set1 == copy1); // false
Copy the code

Example 3:

var map = Map.of("Java".1."Python".2."C".3);
var copy = Map.copyOf(map);
System.out.println(map == copy); // true
var map1 = new HashMap<String, Integer>();
var copy1 = Map.copyOf(map1);
System.out.println(map1 == copy1); // false
Copy the code

Note: Use of and copyOf create set for immutable, can not add, delete, replace, sorting and other operations, or you will quote Java. Lang. UnsupportedOperationException abnormalities, Using the Set of () can’t duplicate elements, Map. Of () can’t duplicate key, otherwise returns the Java. Lang. IllegalArgumentException..

3.Stream enhancement @since 9

Stream is a Java 8 feature with four new methods added in Java 9:

3.1 ofNullable (T T)

This method can accept NULL to create an empty stream

/ / before
Stream.of(null); / / an error
/ / now,
Stream.ofNullable(null);
Copy the code

3.2 takeWhile (Predicate
predicate)

This method determines from the Predicate interface that if true, a new stream will be generated, and terminates when false, regardless of whether the following elements meet the conditions.

Stream<Integer> integerStream = Stream.of(6.10.11.15.20);
Stream<Integer> takeWhile = integerStream.takeWhile(t -> t % 2= =0);
takeWhile.forEach(System.out::println); / / 6, 10
Copy the code

3.3 dropWhile (Predicate
predicate)

This method determines from the Predicate interface that if it is true it will be discarded to generate a new stream, and terminates when it hits false, regardless of whether the following elements meet the criteria.

Stream<Integer> integerStream = Stream.of(6.10.11.15.20);
Stream<Integer> takeWhile = integerStream.dropWhile(t -> t % 2= =0);
takeWhile.forEach(System.out::println); / / 11,15,20
Copy the code

3.4 iterate overloading

Previously using the Iterate method to generate an infinite stream required truncation in conjunction with limit

Stream<Integer> limit = Stream.iterate(1, i -> i + 1).limit(5);
limit.forEach(System.out::println); / / 1, 2, 3, 4, 5
Copy the code

Now overloaded this method adds a judgment argument

Stream<Integer> iterate = Stream.iterate(1, i -> i <= 5, i -> i + 1);
iterate.forEach(System.out::println); / / 1, 2, 3, 4, 5
Copy the code

4.Optional enhancement @since 9

4.1 the stream ()

Returns an empty stream if null, and converts the Optional value to a stream if not null.

// Returns a stream of Optional values
Stream<String> stream = Optional.of("Java 11").stream();
stream.forEach(System.out::println); // Java 11

// Returns an empty stream
Stream<Object> stream = Optional.ofNullable(null).stream();
stream.forEach(System.out::println); // 
Copy the code

4.2 ifPresentOrElse (Consumer
action, Runnable emptyAction)

The ifPresentOrElse method is used to call action (action.accept(value)) on an Optional value if it contains an Optional value. This is consistent with ifPresent; IfPresentOrElse differs from ifPresent in that it has a second argument, emptyAction — ifPresentOrElse calls emptyAction if Optional does not contain a value, Namely emptyAction. The run ().

Optional<Integer> optional = Optional.of(1);
optional.ifPresentOrElse( x -> System.out.println("Value: " + x),() ->
System.out.println("Not Present.")); //Value: 1

optional = Optional.empty();
optional.ifPresentOrElse( x -> System.out.println("Value: " + x),() ->
System.out.println("Not Present.")); //Not Present.
Copy the code

4.3 the or: Supplier
> supplier)

Optional<String> optional1 = Optional.of("Java");
Supplier<Optional<String>> supplierString = () -> Optional.of("Not Present");
optional1 = optional1.or( supplierString);
optional1.ifPresent( x -> System.out.println("Value: " + x)); //Value: Java
optional1 = Optional.empty();
optional1 = optional1.or( supplierString);
optional1.ifPresent( x -> System.out.println("Value: " + x)); //Value: Not Present
Copy the code

5.InputStream enhancement @since 9

String lxs = "java";
try (var inputStream = new ByteArrayInputStream(lxs.getBytes());
	var outputStream = new ByteArrayOutputStream()) {
	inputStream.transferTo(outputStream);
	System.out.println(outputStream); //java
}
Copy the code

HTTP Client API

The API supports both synchronous and asynchronous modes. Here are examples of both modes:

var request = HttpRequest.newBuilder()
	.uri(URI.create("https://www.baidu.com/"))
	.build();
var client = HttpClient.newHttpClient();
/ / synchronize
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
/ / asynchronous
CompletableFuture<HttpResponse<String>> sendAsync = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
// This will block
HttpResponse<String> response1 = sendAsync.get();
System.out.println(response1.body());
Copy the code

Remove the content

  • Com. The sun. The awt. AWTUtilities.
  • Sun. Misc. Unsafe. DefineClass using Java. Lang. Invoke. MethodHandles. Lookup. DefineClass to replace.
  • Thread.destroy() and thread.stop (Throwable) methods.
  • Sun. Nio. Ch. DisableSystemWideOverlappingFileLockCheck properties.
  • Sun. Locale. Formatasdefault properties.
  • JDK SNMP module.
  • Javafx, openJDK was removed from Java10, Oracle Java10 has not removed JavafX, and Java11 removed JavafX.
  • Java Mission Control, once removed from the JDK, will need to be downloaded separately.
  • Root Certificates: Baltimore Cybertrust Code Signing CA, SECOM, AOL and Swisscom.
  • Java EE and CORBA modules deprecated in Java 9 have been removed in Java 11.

Full support for Linux containers (including Docker)

Many applications running in Java virtual machines (including data services such as Apache Spark and Kafka, as well as traditional enterprise applications) can run in Docker containers. The problem with running Java applications in Docker containers is that running JVM applications in Docker containers can cause performance degradation after setting memory size and CPU usage. This is because the Java application is not aware that it is running in a container. With the release of Java 10, this issue has finally been resolved and the JVM can now recognize constraints set by container control groups (CGroups). You can use memory and CPU constraints in containers to manage Java applications directly, including:

  • Adhere to the memory limits set in the container
  • Set the available CPU in the container
  • Set the CPU constraints in the container

The JDK is recommended

Since Java 11 began, Oracle offers a commercial version with paid support. I recommend amazon’s Corretto, which uses the GPL protocol.

Corretto’s Long Term Support (LTS) includes performance enhancements and security updates to Corretto 8, which will be available for free until at least June 2023. Updates are scheduled to be released quarterly.

Amazon will provide a quarterly update of LTS for the Corretto 11 through at least August 2024.

Github download: github.com/corretto/co… Github.com/corretto/co…

The statement

This series of articles is compiled and written by The author of micA, Rumeng Technology. If there is any reference or reprint, please keep the original author and indicate the source.