Java
Runnable interface
multithreading
parameter passing
Java programming

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:

java
1@FunctionalInterface
2public interface Runnable {
3    void run();
4}

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.

java
1public class ParameterizedRunnable implements Runnable {
2    private final String threadName;
3    private final int count;
4
5    public ParameterizedRunnable(String threadName, int count) {
6        this.threadName = threadName;
7        this.count = count;
8    }
9
10    @Override
11    public void run() {
12        for (int i = 0; i < count; i++) {
13            System.out.println(threadName + ": " + i);
14        }
15    }
16}
17
18public class Main {
19    public static void main(String[] args) {
20        Thread thread = new Thread(new ParameterizedRunnable("Thread-1", 10));
21        thread.start();
22    }
23}

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.

java
1public class Main {
2    public static void main(String[] args) {
3        final String message = "Hello from ";
4        Thread thread = new Thread(() -> {
5            System.out.println(message + Thread.currentThread().getName());
6        });
7        thread.start();
8    }
9}

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.

java
1public class Main {
2    public static void main(String[] args) {
3        String threadName = "InnerClassThread";
4        int repeat = 5;
5
6        Runnable runnable = new Runnable() {
7            @Override
8            public void run() {
9                for (int i = 0; i < repeat; i++) {
10                    System.out.println(threadName + ": " + i);
11                }
12            }
13        };
14
15        Thread thread = new Thread(runnable);
16        thread.start();
17    }
18}

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:

StrategyApproachUse-Case
Constructor ParametersCreate a class with fields and pass data through the constructor.When specific data needs to be set during object construction.
Lambda ExpressionsUse lambda expressions with effectively final variables.Ideal for simple tasks where code clarity and conciseness are prioritized.
Inner ClassesUtilize inner or anonymous classes with accessible variables.Useful in older Java versions or when maintaining legacy code.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions