Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 36

Multithreading in Java

In this lesson: Create threads in Java with Thread and Runnable — lifecycle, sleep, join — beginner examples with output. In Hindi & English.

What is multithreading

Multithreading lets a program run several tasks at the same time, each in its own thread. Instead of doing one thing at a time, a program can, for example, download data while still responding to the user. Java has built-in support for creating and running threads.

💡 A thread is a separate path of execution. Multithreading runs several of them together.

Creating a thread (Thread)

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running");
    }
}
// use:
MyThread t = new MyThread();
t.start();     // starts a new thread
Thread running

One way is to extend Thread and override run() with the code to run. Calling start() launches a new thread that executes run().

Creating a thread (Runnable)

class MyTask implements Runnable {
    public void run() {
        System.out.println("Task running");
    }
}
// use:
Thread t = new Thread(new MyTask());
t.start();
Task running

The preferred way is to implement Runnable and pass it to a Thread. This keeps your class free to extend another class, since it does not use up inheritance on Thread.

run vs start

MyThread t = new MyThread();
t.run();     // WRONG: runs on the current thread, no new thread
t.start();   // RIGHT: starts a new thread, then runs run()

This is a classic mistake. start() creates a new thread and calls run() on it. Calling run() directly just executes the code normally, with no concurrency at all.

Thread lifecycle

  • New — created but not started.
  • Runnable — after start(), ready to run.
  • Running — actively executing run().
  • Blocked/Waiting — paused, waiting for a resource or signal.
  • Terminated — finished when run() ends.

Why use threads

  • Responsiveness — keep the UI working while a task runs in the background.
  • Speed — do independent tasks in parallel on multi-core CPUs.
  • Efficiency — use waiting time (like network delays) for other work.

Threads are powerful, but shared data between them needs care — see synchronization.

Common mistakes

  • Calling run() instead of start(). Then no new thread is created.
  • Extending Thread when you need to extend another class. Use Runnable instead.
  • Sharing data without synchronization. This can corrupt data across threads.
  • Expecting a fixed order. Threads run independently; order is not guaranteed.

Summary

  • Multithreading runs several tasks at once, each in a thread.
  • Create threads by extending Thread or implementing Runnable.
  • Prefer Runnable so your class can still extend another.
  • Use start(), not run(), to launch a new thread.
  • A thread moves New → Runnable → Running → (wait) → Terminated.

Next, protect shared data with synchronization, or write tasks compactly with lambda expressions.

Frequently Asked Questions

What is multithreading in Java?
Multithreading means running several parts of a program at the same time, each in its own thread. This lets a program do multiple tasks together — like downloading a file while keeping the interface responsive.
What are the two ways to create a thread?
You can extend the Thread class and override run(), or implement the Runnable interface and pass it to a Thread. Runnable is usually preferred because it leaves your class free to extend something else.
What is the difference between run() and start()?
start() creates a new thread and then runs run() on it, so it runs concurrently. Calling run() directly just runs the code on the current thread, with no new thread — a common mistake.
Should I extend Thread or implement Runnable?
Implement Runnable in most cases. Because Java allows only single class inheritance, extending Thread uses up that one slot. Runnable keeps your class free to extend another class and is more flexible.
What are the states in a thread lifecycle?
A thread moves through New, Runnable, Running, Blocked/Waiting, and Terminated. It starts as New, becomes Runnable after start(), runs, may wait or block, and finally terminates when run() finishes.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicSynchronization Your progress: 36 / 61 (59%) · Lesson 37 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free