Runnable with a parameter?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, concurrency is an essential aspect for developing applications that perform multiple tasks simultaneously. One of the primary constructs used to achieve concurrency is the Runnable interface. However, a common challenge when working with the Runnable interface is passing parameters to the run method. This article explores how to implement Runnable with parameters, providing technical explanations and examples.
Understanding Runnable
The Runnable interface in Java is a functional interface that requires the implementation of a single method, run(). The Runnable interface is designed to be used as the target for threads. The basic syntax is as follows:
Here, the run method does not accept any parameters, which can be limiting when specific data needs to be passed into a thread. Therefore, strategies need to be employed to work around this limitation.
Strategies for Passing Parameters in Runnable
1. Using Constructor Parameters
One of the most common solutions to pass parameters to a Runnable is by using constructor parameters. By defining a custom class that implements Runnable, the constructor can accept parameters that are stored in instance variables. These variables can then be used within the run method.
2. Using Lambda Expressions
With the introduction of lambda expressions in Java 8, creating concise implementations of Runnable became straightforward. However, directly passing parameters through lambda expressions into Runnable still requires either final or effectively final variables. Here is an example demonstrating this:
3. Using Inner Classes
Another way to pass parameters is through inner classes. Although less modern than lambdas, inner classes provide a clear structure and maintain the link to outer variables.
Comparison Table
Below is a table summarizing the key differences and use-cases for each method of passing parameters to a Runnable:
| Strategy | Approach | Use-Case |
| Constructor Parameters | Create a class with fields and pass data through the constructor. | When specific data needs to be set during object construction. |
| Lambda Expressions | Use lambda expressions with effectively final variables. | Ideal for simple tasks where code clarity and conciseness are prioritized. |
| Inner Classes | Utilize inner or anonymous classes with accessible variables. | Useful in older Java versions or when maintaining legacy code. |
Conclusion
Working with Runnable in Java to pass parameters involves understanding how to work within the constraints of the run method. By leveraging constructor parameters, lambda expressions, or inner classes, developers can effectively pass and utilize parameters in concurrent tasks. Understanding these strategies not only enhances the ability to work with Java threads but also reinforces best practices in managing concurrency.
These solutions provide flexibility and clarity, making them essential tools in any Java developer's toolkit for managing concurrent processes effectively.
Related reading
- Running blocking code in Tornado
- Running code in main thread from another thread
- Running code in main thread from another thread
- Running code in main thread from another thread
- Running a MVC app using Spring Boot Hibernate MySql
- Running chron job on a single instance of spring service deployed on aws
- Running each Spring Scheduler in its own thread
- Running javascript from within Asynchronous Content with hinclude in symfony 2

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.