Thursday, 8 June 2017

Java Tutorial: Lambda expression in java[Thread creation using Lambda expression] ~ foundjava


Click here to watch in Youtube :
https://www.youtube.com/watch?v=4qHFrzYJ1XU&list=UUhwKlOVR041tngjerWxVccw



LambdaDemo.java

/**
 * With JDK 8
 * 
 * Java Lambda Expression Example: Creating Thread
 *
 */
public class LambdaDemo
{

    public static void main(String[] args)
    {
        /*
         * Thread Example with lambda.
         * 
         * we are implementing run method by using lambda expression.
         */

        Runnable runnable = () -> {
            System.out.println("Thread is running...");
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

}
Output
Thread is running...
NonLambdaDemo.java
/**
 * Before JDK 8
 *
 */
public class NonLambdaDemo
{

    public static void main(String[] args)
    {
        // Thread Example without lambda
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                System.out.println("Thread is running...");
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

}

Output
Thread is running...
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_thread_create_App.zip?attredirects=0&d=1

No comments:

Post a Comment