Thursday, 9 February 2017

MULTITHREADING USING LAMBDA EXPRESSION IN JAVA

Suppose if we have some class and we are using that class only once, then why to create new class we can go for Anonymous class.

Example –
public class Lambda {
          public static void main(String[] args) throws InterruptedException {
                   Thread t1 = new Thread()
                   {
                             public void run()
                             {
                                      for(int i=0;i<5;i++)
                                      {
                                                System.out.println("in t1");
                                                try {
                                                          Thread.sleep(500);
                                                } catch (InterruptedException e) {}
                                      }
                             }
                   };
                   Thread t2 = new Thread()
                   {
                             public void run()
                             {
                                       for(int i=0;i<5;i++)
                                      {
                                                System.out.println("in t2");
                                                try {
                                                          Thread.sleep(500);
                                                } catch (InterruptedException e) {}
                                      }
                             }
                   };
                   t1.start();
                   Thread.sleep(10);
                   t2.start();
          }
}

Output –
in t1
in t2
in t1
in t2
in t1
in t2
in t1
in t2
in t1
in t2

Note – In the above example we have created anonymous class, but again we can observe that we are using both the objects only once, so it’s better to go for anonymous object using lambda expression.

Example –
public class Lambda {
          public static void main(String[] args) throws InterruptedException {
                   Thread t1 = new Thread(()->{                         
                             for(int i=0;i<5;i++)
                             {
                                      System.out.println("in t1");
                                      try {
                                                Thread.sleep(500);
                                      } catch (InterruptedException e) {}
                             }
                   });                                 
                   Thread t2 = new Thread(() ->
                   {
                             for(int i=0;i<5;i++)
                             {
                                      System.out.println("in t2");
                                      try {
                                                Thread.sleep(500);
                                      } catch (InterruptedException e) {}
                             }
                   });                                 
                   t1.start();
                   Thread.sleep(10);
                   t2.start();
          }
}

Output –
in t1
in t2
in t1
in t2
in t1
in t2
in t1
in t2
in t1
in t2

No comments:

Post a Comment