Pages

Tuesday, December 1, 2020

How To Create A Thread Using Lambda Expressions In Java 8 and Using Runnable With Lambda?

 1. Overview

In this tutorial, we'll learn how to create a thread using lambda expression in java 8 and beyond versions.

Lambda expressions are newly added concept in the JDK 1.8 version and that introduced the functional programming concepts such as assigning the method to the variable.

Important point is that we can directly implementation for the abstract method of interface with java 8 lambda instead of overriding the method by implementing the interface.

How To Create A Thread Using Lambda Expressions In Java 8?


2. Example To Create New Thread Via Runnable Using Lambda in Java 8

In the below program, we are going to create the Thread and implementing the Runnable interface run() method using Lambda Expression.

By using Lambda, we can skip the implements Runnable interface and overriding the run() method which holds the core thread logic.

If you are new to java 8 lambda, you can read the complete set of rules to Lambda Expressions.

And also we can avoid new Runnable() and implementing the run() method using lamdba. Because, once you start writing the code using java 8 then compiler knows that you are using Function Interface Runnable which has only run() method.

So when you pass Runnable lambda to Thread constructor, it treats as passing implementation of Runnable interface with run() method.

Next, Look at the below example program to create a java thread via runnable using lambda expression


package com.javaprogramto.threads.java8;

public class CreateThreadLambda {

	public static void main(String[] args) {

		// Thread creation using java 8 lambda using runnable
		Thread evenNumberThread = new Thread(() -> {
			
			// this logic is implementation of run() method to print only even numbers
			for (int i = 0; i < 20; i++) {
				if (i % 2 == 0) {
					System.out.println("Even Number Thread : "+i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});

		// starting the thread
		evenNumberThread.start();
		
		// Printing the odd numbers from main thread.
		for (int i = 0; i < 20; i++) {
			if (i % 2 == 1) {
				System.out.println("Odd Number Thread : "+i);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
 

Output:

Even Number Thread : 0
Odd Number Thread : 1
Odd Number Thread : 3
Even Number Thread : 2
Odd Number Thread : 5
Even Number Thread : 4
Even Number Thread : 6
Odd Number Thread : 7
Odd Number Thread : 9
Even Number Thread : 8
Odd Number Thread : 11
Even Number Thread : 10
Odd Number Thread : 13
Even Number Thread : 12
Even Number Thread : 14
Odd Number Thread : 15
Even Number Thread : 16
Odd Number Thread : 17
Even Number Thread : 18
Odd Number Thread : 19
 

3. Conclusion

In this article, we've seen how to create a new thread using lambda java 8 with example program to print even and odd number in unordered.

GitHub

Runnable API

Creating a thread using Thread class and Runnable Interface

No comments:

Post a Comment

Please do not add any spam links in the comments section.