Wednesday, 12 October 2022

JAVA 8 - Lambads,Streams - Very Simple Manner

Lambda - Nameless methods we are calling as lambda,

We heard anonymous class as nameless class, What is nameless function ?

Lambda are advanced short cut version of anonymous class.

Usecase: Convertion of Method as Nameless Method (Lambda)

public void fan() {
      System.out.println("Ramesh is megastar fan");
}

As I told lambda means nameless function/method, Now I am going to remove the method name "public void fan"

()  -> {
             System.out.println("Ramesh is megastar fan");
         }

For the single statement we don't require curly brackets {,} and then

() -> System.out.println("Ramesh is megastar fan");


2nd Ex: methods with arguments .

public void add(int a,int b) {
        System.out.println(a+b);
}

As I told lambda means nameless function/method, Now I am going to remove the method name "public void add"

(int a,int b) -> {
                          System.out.println(a+b);
                      }

For the single statement we don't require curly brackets {,} and then

(int a,int b) ->  System.out.println(a+b);
                      
 If the type of the parameter can be decided by compiler automatically based on the context then we can remove types also. 

(a,b) ->  System.out.println(a+b);


3rd Ex: method with arguments and return type

public String str(String str) { 
                return str;
 }

As I told lambda means nameless function/method, Now I am going to remove the method name "public String str"

(String str) -> { return str };

 If the type of the parameter can be decided by compiler automatically based on the context then we can remove types also. 

(str) -> { str };

(str) ->  str ;


Hope you understand lambda secret, As I told lambda is advanced version of anonymous class, Now we will see the scenario.

Concept: Functional Interface.
If an interface contain only one abstract method, such type of interfaces are called functional interfaces and the method is called functional method or single abstract method (SAM). 

Ex: 
1) Runnable -> It contains only run() method 
2) Comparable ->  It contains only compareTo() method 
3) ActionListener -> It contains only actionPerformed() 
4) Callable ->  It contains only call() method 

Inside functional interface in addition to single Abstract method (SAM) we write any number of default and static methods.


Usecase: Convertion of Anonymous Class as Lambda (Anonymous method)

class Test { 
    public static void main(String[] args) { 
        Thread t = new Thread(new Runnable() {
                                                                            public void run() { 
                                                                                 for(int i=0; i<10; i++) { 
                                                                                    System.out.println("Child Thread"); 
                                                                                } 
                                                                           }  
                                              }); 
          t.start(); 
         for(int i=0; i<10; i++) 
                System.out.println("Main thread");  
        }  
}  

In the above example anonymous (Nameless class) -> 

new Runnable() {
           public void run() { 
                  for(int i=0; i<10; i++) { 
                          System.out.println("Child Thread"); 
                  } 
           }  
 }

Converting above one into Lambda (Nameless Function/Method)

For the nameless Function - we only focus on function or method, here function/method is run() method.

 public void run() { 
           for(int i=0; i<10; i++) { 
                     System.out.println("Child Thread"); 
           } 
 }

Remove the function name -> 

 ()  ->  { 
           for(int i=0; i<10; i++) { 
                     System.out.println("Child Thread"); 
           } 
 }

We successfully converted the Anonymous class into Lambda.

Few more scenarios:

interface Calculator { 
        public void sum(int a,int b); 
}

 class Demo implements Calculator { 
        public void sum(int a,int b) { 
             System.out.println("The sum:"+(a+b)); 
        } 


public class Test { 
    public static void main(String[] args) { 
       Calculator  cal = new Demo(); 
        cal .sum(20,5); 
    } 
}


Convert into anonymous class approach

public class Test { 
    public static void main(String[] args) { 
       //Calculator cal = new Demo();
 Calculator cal = new Calculator() {
            public void sum(int a,int b) { 
                                  System.out.println((a+b)); 
                            }
                 };
        cal .sum(20,5); 
    } 
}

Now Converting anonymous class into anonymous function/method (Lambda approach)

public class Test { 
    public static void main(String[] args) { 
       //Calculator cal = new Demo();
  Calculator cal = (a,b) -> { 
                                                 System.out.println(a+b); 
                                               };
        cal .sum(20,5); 
    } 
}

Hope you understand lambda alias nameless function/method concept.   

Tuesday, 4 October 2022

Microservices/Distributed Design Patterns

 I had started posting of the microservices/Distributed design patterns.

Circuit Breaker Section:


Thursday, 4 August 2022

Importance of urandom egd in containers -> java -Djava.security.egd=file:/dev/urandom Importance

 In high distributed, scaled applications , One System/Server needs to connect another Downstream System/Server to establish session, certificates,

Here session/certificates are having random numbers,

Our java security pickup the random number from Linux/Window entropy pool.


What is entropy pool ?

         In any server, user hits the server through keyboard some other noises, Linux server capture those noises put them into entropy pool.


EGD - Entropy Gathered Device


our java security device -> java.security.egd=file:/dev/random


java.security.egd=file:/dev/random

      By default java8 and java11 this property is there in the java.security file, 

If entropy pool having full entries, file:/dev/random works fine,

If entropy pool having no entries, file:/dev/random will waits for long time, it is blocking the thread, it is hitting the performance.


Solution: Always use the java.security.egd=file:/dev/urandom in PCF/Kubernets/Virtual Server.


java.security.egd=file:/dev/urandom

      need to pass this argument in java argument.

If entropy pool having full entries, file:/dev/urandom works fine,

If entropy pool having no entries, file:/dev/urandom will not wait, it will genereate the random number hence it will improve the performance, no blocking of the thread.