create thread in java 8 lamba code example

Example: java anonymous thread lambda

public class LambdaThreadTest {
   public static void main(String args[]) {
      // Child thread
      new Thread(() -> { // Lambda Expression
         for(int i=1; i <= 5; i++) {
            System.out.println("Child Thread: "+ i);
            try {
               Thread.sleep(500);
            } catch(Exception e) {
               e.printStackTrace();
            }
         }
      }).start();
      // Main Thead
      for(int j=1; j < 5; j++) {
         System.out.println("Main Thread: "+ j);
         try {
            Thread.sleep(500);
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
}

Tags:

Java Example