Lambda_Body1.htm
 
  • lambda expression is just a shorter way of
    writing an implementation of a method
    for later execution.
  • Passing a lambda expression to another function allow us to pass not only values but also behaviors and this enable to dramatically raise the level of our abstraction and then project more generic, flexible and reusable API
  • Functional Interface : A Functional Interface is an interface with just one abstract method declared in it.
  • Lambda way:
    new Thread(
    () -> System.out.println("Hello -> from "
    + Thread.currentThread())
    ).start();
  • Anonymous way
    for (int x = 1; x <= 3; x++) {
    new Thread(
    new Runnable() {
    public void run() {
    System.out.println("Hello anonymn  from "
    + Thread.currentThread());
    }
    }).start();

    }
Code : Lambda_Body1.txt


package javatemplate1;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @author Manas9
*/
public class JavaTemplate1 implements Runnable{
@Override
public void run() {
//run method for
for (int x = 1; x <= 3; x++) {
System.out.println(x + " Thread "
+ Thread.currentThread());}
//Lammbda way
for (int x = 1; x <= 3; x++) {
new Thread(
() -> System.out.println("Hello -> from "
+ Thread.currentThread()) ).start();
}
} // end of void run
public static void main(String[] args) {
// TODO code application logic here
System.out.println("main block executing ");
JavaTemplate1 run1 = new JavaTemplate1();
Thread t1 = new Thread(run1);
t1.start();
}
}
 

Runtime View :