Java Programs›Multithreading›Create a Thread Using Runnable

Create a Thread Using Runnable in Java

intermediate·  Multithreading  ·  Concurrency

Problem

Runnable is a one-method interface describing a task; passing it to a Thread constructor separates what to do (the task) from how it runs (the thread).

Run a download task on a separate, named thread and report when it has finished.

Input
task: download report.pdf
Output
download-thread downloading report.pdf

Java Program

Java
public class RunnableExample { static class Downloader implements Runnable { private final String file; Downloader(String file) { this.file = file; } @Override public void run() { System.out.println(Thread.currentThread().getName() + " downloading " + file); } } public static void main(String[] args) throws InterruptedException { Thread t = new Thread(new Downloader("report.pdf"), "download-thread"); // task + name t.start(); t.join(); System.out.println("Download task complete"); } }

Output

download-thread downloading report.pdf Download task complete

Core Logic

Implementing Runnable instead of extending Thread keeps the task an ordinary class that can carry its own data and still extend something else.

How It Works
  1. 1Downloader implements Runnable stores the file name and prints it inside run().
  2. 2new Thread(task, "download-thread") pairs the task with a thread and gives it a readable name.
  3. 3t.start() runs the task on that thread.
  4. 4t.join() waits for it before main prints the completion message.
Thread.currentThread().getName() inside the task prints download-thread, showing which thread did the work.
💡

Key Point: Prefer Runnable (or Callable) over extending Thread: Java has single inheritance, and the same task object can later be submitted to an ExecutorService unchanged.

Key Concepts

RunnableThread(Runnable, String)join()

Approach 2: Runnable as a Lambda

Java
public class RunnableLambda { public static void main(String[] args) throws InterruptedException { Runnable task = () -> System.out.println(Thread.currentThread().getName() + " says hello"); Thread t = new Thread(task, "lambda-thread"); t.start(); t.join(); System.out.println("Main thread: " + Thread.currentThread().getName()); } }

Output

lambda-thread says hello Main thread: main

Core Logic

Because Runnable has a single abstract method, a lambda can supply the task without a separate class.

How It Works
  1. 1() -> System.out.println(...) is the whole task.
  2. 2The lambda is passed to new Thread(task, "lambda-thread").
  3. 3After join(), main prints its own thread name for comparison.
The task reports lambda-thread, and the last line shows the main thread is called main.
💡

Key Point: Lambdas are ideal for short tasks; give longer tasks their own class so they can be tested and reused.

Key Concepts

lambda expressionfunctional interface

Interview Questions

What's the difference between Runnable and Callable?

Runnable.run() returns nothing and can't throw checked exceptions. Callable.call() returns a value and may throw, and it's used with executors and Futures.

Why is implementing Runnable preferred over extending Thread?

It keeps the task separate from the thread, lets the class extend something else, and allows the same task to run in a thread pool.

Learn the Concept

Related Programs