Java 8 – Implement Runnable with Lambda Expressions

Introduction

Java 8 introduced lambda expressions, which provide a more concise and readable way to write functional interfaces like Runnable. The Runnable interface is a functional interface with a single abstract method run(), which is often used to define tasks that can be executed by a thread. Before Java 8, implementing a Runnable required creating an anonymous class, but with lambda expressions, the syntax is greatly simplified.

In this guide, we’ll explore how to implement Runnable using lambda expressions in Java 8, showing you how to simplify your code and make it more expressive.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Example 1: Implementing Runnable Without Lambda Expressions
    • Example 2: Implementing Runnable with Lambda Expressions
    • Example 3: Executing Multiple Tasks Using Lambda Expressions
  • Conclusion

Problem Statement

In Java, the Runnable interface is often used to define tasks that will be executed in a separate thread. Traditionally, this involved creating anonymous classes, which can be verbose and harder to read. The goal is to implement Runnable using lambda expressions to simplify the code.

Example:

  • Problem: Writing and managing Runnable tasks with anonymous classes can be verbose and less readable.
  • Goal: Use lambda expressions to implement Runnable in a concise and clean way.

Solution Steps

  1. Implement Runnable Using Anonymous Classes: Demonstrate the traditional way of implementing Runnable without lambda expressions.
  2. Simplify with Lambda Expressions: Use lambda expressions to implement Runnable in a more concise manner.
  3. Execute Multiple Tasks: Demonstrate how to use lambda expressions to run multiple Runnable tasks.

Java Program

Example 1: Implementing Runnable Without Lambda Expressions

Before Java 8, implementing Runnable typically involved creating an anonymous inner class. Here’s how it was done:

/**
 * Java 8 - Implement Runnable Without Lambda Expressions
 * Author: https://www.rameshfadatare.com/
 */
public class RunnableExample1 {

    public static void main(String[] args) {
        // Implementing Runnable using an anonymous class
        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println("Task executed using anonymous class.");
            }
        };

        // Start the thread
        Thread thread = new Thread(task);
        thread.start();
    }
}

Output

Task executed using anonymous class.

Explanation

  • Anonymous Class: The Runnable interface is implemented using an anonymous class, which requires a significant amount of boilerplate code for a simple task.

Example 2: Implementing Runnable with Lambda Expressions

Java 8 allows you to replace the anonymous class with a lambda expression, significantly simplifying the code.

/**
 * Java 8 - Implement Runnable with Lambda Expressions
 * Author: https://www.rameshfadatare.com/
 */
public class RunnableExample2 {

    public static void main(String[] args) {
        // Implementing Runnable using a lambda expression
        Runnable task = () -> System.out.println("Task executed using lambda expression.");

        // Start the thread
        Thread thread = new Thread(task);
        thread.start();
    }
}

Output

Task executed using lambda expression.

Explanation

  • Lambda Expression: The lambda expression () -> System.out.println("Task executed using lambda expression.") replaces the anonymous class. The code is now more concise and easier to read.

Example 3: Executing Multiple Tasks Using Lambda Expressions

You can use lambda expressions to create and execute multiple Runnable tasks more efficiently.

/**
 * Java 8 - Implement Multiple Runnables with Lambda Expressions
 * Author: https://www.rameshfadatare.com/
 */
public class RunnableExample3 {

    public static void main(String[] args) {
        // Implementing multiple Runnable tasks using lambda expressions
        Runnable task1 = () -> System.out.println("Task 1 executed.");
        Runnable task2 = () -> System.out.println("Task 2 executed.");
        Runnable task3 = () -> System.out.println("Task 3 executed.");

        // Start the threads
        new Thread(task1).start();
        new Thread(task2).start();
        new Thread(task3).start();
    }
}

Output

Task 1 executed.
Task 2 executed.
Task 3 executed.

Explanation

  • Multiple Tasks: By using lambda expressions, multiple Runnable tasks are easily defined and executed. Each task is started in its own thread, demonstrating the flexibility of using lambdas with Runnable.

Conclusion

Implementing Runnable with lambda expressions in Java 8 simplifies the process of defining and executing tasks in separate threads. By reducing boilerplate code and improving readability, lambda expressions make it easier to manage concurrent tasks in your applications. Whether you’re implementing a single Runnable or managing multiple tasks, lambda expressions provide a clean and concise way to work with functional interfaces like Runnable.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top