Skip to main content

Posts

Showing posts with the label Java 25

Java 25 Feature: Structured Task Scope

  Structured Task Scope Copy Code So in the above code what is happening, code line var scope = new StructuredTaskScope<>() is opening a scop (consider it as a container) and any task (thread) which is being opened or executed in this scop will be consistent to other tasks in that same scope. scope.join() is making the both the tasks to execute and finish together.  scope.throwIfFailed() which means if any task is failed then other executed tasks will also come on halt. e.g take this scenario where you want to update user password in diff servers. if password update failed on any server then we can discard the whole process so that user doesn't have to use old and new password on diff servers that would be very sus infra. import java.util.concurrent.StructuredTaskScope; public class StructuredExample { static String fetchUser() { try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().int...

Java 25 Feature: Flexible Code Compilation by Pran Sukh

Java 25 Features Java 25 Example: Primitive Pattern Matching Copy /* In java 25 you need explicit definition of class and main method, just for start it's easy to understand the execution. * But in real Object-Oriented applications you cannot go without classes and inheritance */ void main(){ int i = 3; char any = 'P'; oldWayOfUnBoxing(i); oldWayOfUnBoxing(any); checkObject(i); checkObject(any); } static void oldWayOfUnBoxing(Object obj) { if (obj instanceof Integer) { int i = ((Integer) obj).intValue(); // First obj is boxed to Integer then unboxed to int with .intValue of Integer class log("Primitive obj of type int is converted/unboxed to int with Integer class"); } if (obj instanceof Character) { char c = ((Character) obj).charValue(); // First ob...