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.
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
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();
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 ofstart(). 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(), notrun(), 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?
What are the two ways to create a thread?
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.