Thursday, 20 September 2018

Lambda Expression Concept With Runnable Interface(Functional Interface)

//Without Lambda Expression package govind; public class MyThread implements Runnable{ @Override public void run() { for(int i=0;i<10;i++) { System.out.println("Child Thread"); } } } package govind; public class MythreadTest { public static void main(String[] args) { Runnable r=new MyThread(); Thread t=new Thread(r); t.start(); for(int i=0;i<10;i++) { System.out.println("Main Thread"); } } } o/p:----------------------------------------------------------ist time-------------------------------------------------------------- Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Main Thread Main Thread -------------------------------------------------------------2nd time----------------------------------------------------------------------- Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Child Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Main Thread Random o/p (dont expect) ================================================================================================================================================================================= //With Lambda Expression package govind; public class MyLambdaThread { public static void main(String[] args) { Runnable r=()->{ for(int i=0;i<10;i++) { System.out.println("Lambda Child Thread"); } }; Thread t=new Thread(r); t.start(); for(int i=0;i<10;i++) { System.out.println("Lambda Main Thread"); } } } o/p:-----------------------------------------------------------ist time------------------------------ Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread ------------------------------------------------------------2nd time---------------------------------------------- Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Child Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Lambda Main Thread Random o/p (dont expect)

No comments:

Post a Comment