Creating Threads using Lambda Expressions
Table of Contents
- Introduction
- What is Lambda in Java?
- Creating Threads Using Lambda Expressions
- Step-by-Step Guide to Implementing Threads Using Lambda
- Code Walkthrough
- Conclusion
1. Introduction
Java introduced Lambda expressions in version 8, which revolutionized how we write code by simplifying function expressions.
One key application of Lambda expressions is creating threads. This article will guide you through the concept of creating threads
using Lambda expressions, providing a practical example based on a project file.
In multi-threaded applications, traditional thread creation requires a more verbose syntax, such as implementing the
Runnable interface. With Lambda expressions, we reduce boilerplate code and write more concise and readable code.
2. What is Lambda in Java?
Lambda expressions provide a clear and concise way to represent a function interface in Java. Before Lambdas, creating an instance
of a functional interface required either an anonymous class or an external class implementation. With Lambda, you can express
these interfaces with a more streamlined syntax.
Pros of Using Lambda:
- Concise and expressive syntax
- Improved code readability
- Reduces boilerplate code
- Functional programming support
Cons of Using Lambda:
- Reduced clarity for beginners due to implicit interface implementations
- Can lead to overuse in inappropriate contexts
Comparison between Traditional and Lambda Thread Creation
Approach | Syntax Overhead | Readability | Code Size |
---|---|---|---|
Traditional Runnable | High | Medium | Larger |
Lambda Expression | Low | High | Smaller |
When to Use:
- Use Lambda when dealing with functional interfaces, such as Runnable for thread creation.
- Suitable for scenarios where concise code is prioritized without sacrificing functionality.
3. Creating Threads Using Lambda Expressions
Before Lambda expressions, threads were created by either extending the Thread class or implementing the
Runnable interface. Here’s how it looked:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MyRunnable implements Runnable { @Override public void run() { System.out.println("Thread is running"); } } public class Main { public static void main(String[] args) { Thread thread = new Thread(new MyRunnable()); thread.start(); } } |
This code, while functional, requires us to create separate classes or anonymous implementations for simple tasks.
Lambda Approach:
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { Thread thread = new Thread(() -> { System.out.println("Statement 01"); System.out.println("Statement 02"); }); thread.start(); } } |
In this approach:
- The Lambda expression () -> {} replaces the Runnable interface’s run() method.
- The code is concise, removing the need for an external class or an anonymous inner class.
4. Step-by-Step Guide to Implementing Threads Using Lambda
Step 1: Define the Thread
We create a Thread object and pass the Lambda expression inside the constructor.
The Lambda expression serves as the body of the Runnable interface’s run() method.
Step 2: Add Statements
Within the Lambda expression, you can define the tasks to be executed by the thread. In the example, we print two statements:
1 2 |
System.out.println("Statement 01"); System.out.println("Statement 02"); |
Step 3: Start the Thread
To initiate the thread, use the start() method:
1 |
thread.start(); |
The thread will execute the code inside the Lambda expression in a separate execution path.
5. Code Walkthrough
Here is the complete code provided in the project file:
1 2 3 4 5 6 7 8 9 10 |
package org.studyeasy; public class Main { public static void main(String[] args) { Thread thread = new Thread(() -> { System.out.println("Statement 01"); System.out.println("Statement 02"); }); thread.start(); } } |
Explanation:
- Thread Creation: A new thread is created using the Thread class, and a Lambda expression is passed as the Runnable target.
- Lambda Expression: The Lambda expression () -> {} contains two print statements, which will be executed when the thread starts.
- Thread Execution: The thread.start() method initiates the thread’s execution, printing “Statement 01” and “Statement 02”.
Output:
1 2 |
Statement 01 Statement 02 |
6. Conclusion
Creating threads using Lambda expressions in Java is a highly efficient and concise method. It simplifies the syntax while preserving the full functionality of thread creation.
This approach not only improves readability but also reduces the complexity associated with traditional thread creation methods.
By leveraging the power of Lambda expressions, developers can write more maintainable and expressive code, especially in multi-threaded environments.