Runnable interface in Java

Runnable interface in Java

The Runnable interface represents a task that can be executed asynchronously, typically used with threads. It provides a single method, run(), that contains the code to be executed.

1. Basic Usage

Here's a basic example of implementing the Runnable interface:

public class SimpleRunnable implements Runnable { @Override public void run() { System.out.println("Running in a separate thread!"); } public static void main(String[] args) { Thread thread = new Thread(new SimpleRunnable()); thread.start(); System.out.println("Main thread!"); } } 

When you run the code, you might get different outputs since thread execution order is not guaranteed. A possible output is:

Main thread! Running in a separate thread! 

2. Using Lambda Expressions

Java 8 and later versions allow you to use lambda expressions to create instances of interfaces with a single abstract method (known as functional interfaces). Since Runnable is a functional interface, you can use a lambda:

public class LambdaRunnable { public static void main(String[] args) { Runnable task = () -> { System.out.println("Running using a lambda!"); }; Thread thread = new Thread(task); thread.start(); } } 

3. Using Anonymous Classes

Before the introduction of lambda expressions in Java 8, anonymous inner classes were a popular way to instantiate Runnable:

public class AnonymousRunnable { public static void main(String[] args) { Runnable task = new Runnable() { @Override public void run() { System.out.println("Running using an anonymous class!"); } }; Thread thread = new Thread(task); thread.start(); } } 

4. Parallel Execution

Multiple Runnable tasks can be executed in parallel using multiple threads:

public class ParallelExecution { public static void main(String[] args) { Runnable task1 = () -> { System.out.println("Task 1 running!"); }; Runnable task2 = () -> { System.out.println("Task 2 running!"); }; Thread thread1 = new Thread(task1); Thread thread2 = new Thread(task2); thread1.start(); thread2.start(); } } 

5. Differences Between Runnable and Thread

  • Purpose: Runnable is an interface that represents a task, while Thread is a class representing an actual thread of execution.

  • Extensibility: Java doesn't support multiple inheritance. So, if your class extends Thread, it can't extend any other class. But if your class implements Runnable, it's free to extend another class.

  • Reusability: The Runnable task can be passed to different threads, making it more versatile. The Thread class, once started, can't be restarted, so you would have to create a new instance to execute the same code again.

6. Executor Framework

Java provides the Executor framework (in java.util.concurrent) which is a higher-level replacement for the traditional way of managing threads, and it primarily uses the Runnable interface. For example:

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ExecutorExample { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.submit(() -> { System.out.println("Task 1 executed via Executor!"); }); executorService.submit(() -> { System.out.println("Task 2 executed via Executor!"); }); executorService.shutdown(); } } 

Conclusion

The Runnable interface provides a clean way to represent tasks that can be executed concurrently. Whether you're directly working with threads or using the Executor framework, understanding Runnable is crucial to effective multi-threaded programming in Java.


More Tags

multiple-select d3.js google-play quaternions alphanumeric out wizard notimplementedexception aws-sdk-ruby android-activity

More Programming Guides

Other Guides

More Programming Examples