The September 19th of 2023 a new Java version will be released. Java 21.
It will be an LTS version, a Long-Term Support. This means the version will be maintained for at least 3 years and 6 years for security patches.
But the most important, is what this version includes.
The String Templates (preview)
With the String Templates I can now build a String with variables values without the need to concatenating many Strings.
String customerName = "Sergio";
String str = STR."Hello \{customerName}!";
But I can go further calling methods or operations.
String str1 = STR."Hello \{customer.getName()}";
String str2 = STR."My age is \{Year.now().getValue() - customer.getBirthYear()}";
The Record Pattern
With the instanceof I can now map the values of the object into a Record and directly use the fields name.
if (o instanceof Customer (String name, int age)) {
System.out.println(name);
}
If the object o is an instance of Customer, it is directly mapped into an instance Customer, and I can directly use the fields name and age inside the if-condition.
The Pattern Matching For Switch
The Switch selector was already upgraded in the previous versions of Java. Now it includes the possibility to match reference types.
switch (o) {
case Customer -> { /* handle customer */ }
case Admin -> { /* handle admin */ }
case null -> throw new AppException("User must not be null");
default -> throw new AppException("Unknown user type");
}
I don’t need anymore a list of if-conditions with instanceof. I can handle this with a simple switch-case.
And The Virtual Threads
And the last but not the least, the Virtual Threads.
I can now create virtual lightweight threads which are not bounded to the platform threads.
Let’s see the behavior of an older version of Java.
try (var executor = Executors.newThreadPerTaskExecutor(Executors.defaultThreadFactory())) {
IntStream.range(0, 10_000).forEach(i -> {
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1));
System.out.println("Finished");
return i;
});
});
}
-> | Exception java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
In this code, I create 10.000 threads which wait 1 second to print “Finished”. But my laptop does not have 10.000 CPU threads. So it will cause an OutOfMemoryError.
The Virtual Threads are not bounded to the CPU and are lightweight, this allows me to create much more threads than before.
Let’s see the previous example using Virtual Threads.
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 10_000).forEach(i -> {
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1));
System.out.println("Finished");
return i;
});
});
}
This time, I have no exception thrown. I will see “Finished” printed 10.000 times in console until the application finishes.
The Virtual Threads was a feature expected from many versions. It’s the availability to work in concurrent programming with no limits.
If you want to learn more about good quality code, make sure to follow me on Youtube.



Leave a comment