Popular
Data Science
Technology
Finance
Management
Future Tech
The ‘Runnable’ interface in Java is a functional interface, which is used to represent a task that can be executed concurrently by a thread. Implementing the ‘Runnable’ interface allows you to define the source code that should be executed in a separate thread. This article explores the steps to implement the ‘Runnable’ interface and discusses common errors that developers might encounter while implementing the Runnable Interface in Java.
The ‘Runnable’ interface is a functional interface, meaning it has a single abstract method, “run().” This method defines the code that constitutes the task to be executed by a thread. The interface is simple yet powerful, providing a way to encapsulate a unit of work that can be worked on by multiple threads concurrently.
Syntax of the Runnable interface
The ‘run()’ method is the single abstract method defined by the ‘Runnable’ interface. It is where you place the source code that should be executed when a thread is started. The method is central to the Java threading model, providing a simple and standardized way to define tasks that can be run concurrently.
We can create a new thread using the Runnable interface by the following steps in Java.
Let’s implement the Runnable Interface in Java language. In this program, we will implement the Runnable interface to the MyRunnable class and override the run() method. The code inside run() is what the thread will execute.
The following program demonstrates the Runnable Interface
Program
Output
The overridden run() method is not allowed to throw checked exceptions, such as Exception, which are checked at compile time. It can only throw unchecked exceptions that occur at runtime, like RuntimeException. Specifically, the run() method can only throw RuntimeException or any exceptions that are subclasses of RuntimeException.
Checked Exception Inside run() Method
After compiling the code class throws the below error because exception is a checked exception.
The following source code does not throw any run-time error because the run() method can throw unchecked exceptions. The following program demonstrates the Unchecked Exception
The Runnable interface in Java is a key part of the threading framework. It allows for concurrent execution of source code. Let’s see some use cases of the Runnable Interface in Java language.
Criteria | Thread Class | Runnable Interface |
Inheritance | Requires extending the ‘Thread class | Implements the Runnable interface |
Multiple Inheritance | Not possible to extend another class | Allows extending another class. |
Resource Sharing | Each thread has its own instance, making sharing resources difficult | The multiple threads can share the same instance. |
Decoupling of Task and Thread | Task and thread are tightly coupled | Task and thread are decoupled, offering more flexibility. |
Reusability | Less reusable since the task is tied to the thread | The more reusable as the task can be passed to multiple threads. |
Memory Overhead | Higher due to additional thread object overhead | Lower, as only one instance of ‘Runnable’ is required. |
The ‘Runnable’ interface in Java is a powerful tool for implementing multithreading. Separating the task from the thread provides greater flexibility, allowing your class to extend other classes and enabling resource sharing among multiple threads.
When a developer implements a Runnable interface, they must be aware of potential errors like missing the ‘run’ method or incorrectly managing shared resources. However, when done correctly, using the ‘Runnable’ interface leads to cleaner, more modular source code, which is especially beneficial in complex, large-scale applications.
The DevOps Playbook
Simplify deployment with Docker containers.
Streamline development with modern practices.
Enhance efficiency with automated workflows.
Popular
Data Science
Technology
Finance
Management
Future Tech
Accelerator Program in Business Analytics & Data Science
Integrated Program in Data Science, AI and ML
Certificate Program in Full Stack Development with Specialization for Web and Mobile
Certificate Program in DevOps and Cloud Engineering
Certificate Program in Application Development
Certificate Program in Cybersecurity Essentials & Risk Assessment
Integrated Program in Finance and Financial Technologies
Certificate Program in Financial Analysis, Valuation and Risk Management
© 2024 Hero Vired. All rights reserved