Lambda_Anonymous1
 

  • public class JavaTemplate1 implements Runnable{

    @Override
    public void run() {
    //
    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(
    new Runnable() {
    public void run() {
    System.out.println("Hello annonym from "
    + Thread.currentThread());
    }
    }).start();

    }
    //
    } // end of void run


  • Lambda way


  •  
====== Annonymous class ====
public class JavaTemplate1 implements Runnable{

@Override
public void run() {
//
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(
new Runnable() {

public void run() {
System.out.println("Hello annonym from "
+ Thread.currentThread());

}
}).start();

}
//
} // end of void run


===== Using Lambda code =========

public class JavaTemplate1 implements Runnable{
@Override
public void run() {
//
for (int x = 1; x <= 3; x++) {
System.out.println(x + " Thread "
+ Thread.currentThread());}
//Lammbda/Annonymous way
for (int x = 1; x <= 3; x++) {
new Thread(
() -> {
System.out.println("Hello Lambda from "
+ Thread.currentThread())
;
}).start();

}
//
}

Complete Code : Lambda_Anonymous1.txt Using as anonymous class


 

 

Code Using lambda : Lambda_Anonymous1B.txt