Java Runnable with a parameter
Codemia Knowledge Hub ·
Runnable.run() takes no arguments, so you cannot pass a parameter to it directly. Instead, give the data to the Runnable before it runs: through the constructor of a class that implements Runnable, or by capturing final or effectively final variables in a lambda or anonymous inner class.
Runnable is one of the main building blocks for running tasks concurrently in Java. It is a functional interface with a single method, run(), and is designed to be the target of a thread:
Because run() accepts no parameters, any data the thread needs has to reach the object some other way. The three common workarounds follow.
Passing values through a constructor
Write a class that implements Runnable, accept the values in its constructor, store them in instance fields and read those fields inside run(). This is the most common approach, and it works in every Java version.
Capturing variables in a lambda
Since Java 8, a lambda is the shortest way to write a Runnable. The variables it uses from the enclosing scope must be final or effectively final.
Capturing variables in an anonymous inner class
An anonymous inner class can read variables from the enclosing method too. It is older and more verbose than a lambda, but the structure is explicit and it keeps a clear link to the outer variables.
Choosing an approach
All three let a thread work with data that run() cannot receive as an argument. Pick based on how much state the task carries and how often you reuse it:
| 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. |
Related reading
- Running blocking code in Tornado
- Running code in main thread from another thread
- Running each Spring Scheduler in its own thread
- Running javascript from within Asynchronous Content with hinclude in symfony 2
- Running a MVC app using Spring Boot Hibernate MySql
- Running chron job on a single instance of spring service deployed on aws
- Running Keras model for prediction in multiple threads
- Running MSIL on GPU
System design interview problems on this topic
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough
- Design Ticketmaster: solution and walkthrough
- Design a Flash Sale System: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.