by BehindJava

WAP to create a thread using lambda expression and print “Hello World” in Java

Home » java » WAP to create a thread using lambda expression and print “Hello World” in Java

In this tutorial, we are going to learn how “hello” is printed by creating a thread using lambda expression. Java provides an annotation @FunctionalInterface, which is used to declare an interface as functional interface.

It is not mandatory to use this annotation, an interface with single abstract method is by default considered as functional interface.

Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface.

The actual way of creating the thread by implementing the runnable interface without using Java 8 is this.

class ThreadCheck implements Runnable{  
public void run(){  
System.out.println("Hello World...");  
}  
  
public static void main(String args[]){  
ThreadCheck m1=new ThreadCheck();  
Thread t1 =new Thread(m1);   // Using the constructor Thread(Runnable r)  
t1.start();  
 }  
}  

Output:

Hello World...

Here Runnable interface consists of only one abstract method i.e., public abstract void run() it can be considered as a functional interface and can implement lambda expression which enables functional programming and reduces the extra lines of code.

Sample Code Snippet:

public class ThreadCheck {
	public static void main(String[] args)
	{
        // providing implementation for runnnable interface which 
        // has single abstract method i.e. run()		Runnable r=()-> {
			 {
				System.out.println("Hello World...");
			 }	
		     };
		Thread t1=new Thread(r);		t1.start();
	}

Output:

Hello World...

You can have a look at Runnable interface below here.

package java.lang;

/**
 * The {@code Runnable} interface should be implemented by any
 * class whose instances are intended to be executed by a thread. The
 * class must define a method of no arguments called {@code run}.
 * <p>
 * This interface is designed to provide a common protocol for objects that
 * wish to execute code while they are active. For example,
 * {@code Runnable} is implemented by class {@code Thread}.
 * Being active simply means that a thread has been started and has not
 * yet been stopped.
 * <p>
 * In addition, {@code Runnable} provides the means for a class to be
 * active while not subclassing {@code Thread}. A class that implements
 * {@code Runnable} can run without subclassing {@code Thread}
 * by instantiating a {@code Thread} instance and passing itself in
 * as the target.  In most cases, the {@code Runnable} interface should
 * be used if you are only planning to override the {@code run()}
 * method and no other {@code Thread} methods.
 * This is important because classes should not be subclassed
 * unless the programmer intends on modifying or enhancing the fundamental
 * behavior of the class.
 *
 * @author  Arthur van Hoff
 * @see     java.lang.Thread
 * @see     java.util.concurrent.Callable
 * @since   1.0
 */
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface {@code Runnable} is used
     * to create a thread, starting the thread causes the object's
     * {@code run} method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method {@code run} is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}