Lambda expressions (Project Lambda - JSR 335) are a new and important feature of the upcoming Java SE 8 platform (JSR 337). They can be used to represent one method interface (also known as functional interface) in a clear and concise way using an expression in the form of:

(argument list) -> body

In this article, we see how Lambda expressions can simplify the creation of a new thread.

 

1. Create a Java thread via Runnable using Classic Code

Before Java 8, we create and start a thread by creating an anonymous class that implements the Runnable interface, as shown in the following code:

Runnable task1 = new Runnable(){

	@Override
	public void run(){
		System.out.println("Task #1 is running");
	}
};


Thread thread1 = new Thread(task1);
thread1.start();
Or pass the anonymous class into the Thread’s constructor:

Thread thread1 = new Thread(new Runnable() {
	@Override
	public void run(){
		System.out.println("Task #1 is running");
	}
});

thread1.start();
 

2. Create a Java thread via Runnable using Lambda expression

With Lambda expressions come with Java 8, the above code can be re-written more concisely. For example:

// Lambda Runnable
Runnable task2 = () -> { System.out.println("Task #2 is running"); };

// start the thread
new Thread(task2).start();
It’s much more simple, isn’t it? By using Lambda expression, you don’t have to write the boilerplate code: declarations of the anonymous class and the run() method.

And the following code snippet is for test program that demonstrates creating threads using both classic and Lambda approaches:

package net.codejava.lambda;

/**
 * This simple program demonstrates how to use Lambda expressions to create
 * and run threads.
 *
 * @author www.codejava.net
 */
public class RunnableLambdaExample {
	public static void main(String[] args) {
		System.out.println(Thread.currentThread().getName() + ": RunnableTest");

		// Anonymous Runnable

		Runnable task1 = new Runnable(){

		  @Override
		  public void run(){
			System.out.println(Thread.currentThread().getName() + " is running");
		  }
		};


		// Passing a Runnable when creating a new thread
		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run(){
				System.out.println(Thread.currentThread().getName() + " is running");
			}
		});


		// Lambda Runnable
		Runnable task3 = () -> {
			System.out.println(Thread.currentThread().getName() + " is running");
		};

		Thread thread1 = new Thread(task1);

		thread1.start();
		thread2.start();

		new Thread(task3).start();

	}
}


Output of this program may be different on each run because there are 3 threads started simultaneously. For example:
Run #1:

main: RunnableTest
Thread-1 is running
Thread-0 is running
Thread-2 is running
Run #2:

main: RunnableTest
Thread-1 is running
Thread-2 is running
Thread-0 is running
 

Related Tutorials:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (RunnableLambdaExample.zip)RunnableLambdaExample.zip[Java source file]1 kB

Add comment

   


Comments 

#20Ram2024-02-12 20:01
Quoting Laxman Jaygonde:
How does Thread know that lambda expression is implementing Runnable interface and not any other interface?


What is there on the left side?
Quote
#19simplechinesefood.co2021-09-15 07:45
Java's multi thread is more strong than Python
Quote
#18Mattl2021-05-23 14:08
Thanks for your work, it really helped me!
Quote
#17Laxman Jaygonde2019-03-23 13:44
How does Thread know that lambda expression is implementing Runnable interface and not any other interface?
Quote
#16Tuan2017-04-20 12:27
Quoting georg:
why i get errors if i try to use lambda ???

Have you install JDK 8 on your computer?
Quote