Nearby lessons

67 of 125

Java - Multithreading Overview

📌 What You Will Learn
  • What a thread is and how it is different from a process
  • Two ways to create a thread: Thread class and Runnable interface
  • The thread life cycle (states)
  • How threads cooperate: synchronized, wait and notify
  • The modern way: Executors, Callable and lambdas

Multithreading Overview is a core concept of the Java language. This lesson explains What is a Thread?, Two Ways to Create a Thread and Process vs Procedure vs Thread with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

What is a Thread?

A thread is a small, independent unit of work running inside a program. A program that can run several threads at the same time is a multithreaded program.

Real-life example: while you are downloading a big file (thread 1), you can also chat with a friend (thread 2) and listen to music (thread 3) — all at the same time. In a program, threads let these tasks run together instead of one after another.

PointProcessThread
What is itA running programA unit of work inside a program
MemoryEach process has its own separate memoryThreads share the program's memory
CostHeavy, slow to createLight, fast to create
CommunicationComplicated (IPC)Easy (they share data)
ExampleRunning Word, running ChromeTwo tabs inside Chrome
In simple words: A process is a running program with its own memory; a thread is a small unit of work inside that process. Threads share the program's memory, which is why they are light and fast to create.

Two Ways to Create a Thread

Method 1 — Extend the Thread class

Output may differ every run — that is the magic of threads. Both loops run at the same time, so the printed order is not fixed.

Method 2 — Implement the Runnable interface (Recommended)

Java does not allow a class to extend more than one class. If your class already extends some class, it cannot also extend Thread. The solution: implement Runnable and give the job to a Thread object.

Example02
JCode Cell
1class MyThread extends Thread {
2 public void run() { // override run() - the thread's job
3 for (int i = 1; i <= 5; i++) {
4 System.out.println("Child: " + i);
5 }
6 }
7}
8 
9class Test {
10 public static void main(String[] args) {
11 MyThread t = new MyThread();
12 t.start(); // start() creates the thread and calls run()
13 for (int i = 1; i <= 5; i++) {
14 System.out.println("Main: " + i);
15 }
16 }
17}
Output
Main: 1 Child: 1 Child: 2 Main: 2 Main: 3 Child: 3 ... (order varies)

Two Ways to Create a Thread

Trainer's Note: Modern shortcut (Java 8+): because Runnable has only one method, we can use a lambda: Thread t = new Thread(() -> System.out.println("Hi"));. Much shorter. Full details in the Java 8 chapter.
In simple words: Only `start()` creates a new thread; calling `run()` directly is just a normal method call. If you write t.run() the job executes in the current thread, so no real multithreading happens.
Example03
JCode Cell
1class MyTask implements Runnable {
2 public void run() {
3 System.out.println("Task is running in thread: " + Thread.currentThread().getName());
4 }
5}
6 
7class Test {
8 public static void main(String[] args) {
9 MyTask task = new MyTask();
10 Thread t = new Thread(task); // attach the job to a thread
11 t.start();
12 }
13}
Output
Task is running in thread: Thread-0

Process vs Procedure vs Thread

Three similar words that students confuse. The classic material separates them clearly:

WordMeaning
ProcedureA set of instructions written to do one task (a function). It is just code, not running.
ProcessA running program — one procedure (or more) actually executing in memory.
ThreadA single path of execution inside a process. One process can have many threads.

Example: Notepad is a process. Inside it, typing, spell-check and autosave can each be a thread running at the same time.

📝 Key Takeaways
  • A thread is a small unit of work; a process is a running program.
  • Create a thread by extending Thread or (better) implementing Runnable.
  • Life cycle: NEW -> RUNNABLE -> BLOCKED/WAITING/TIMED_WAITING -> TERMINATED.
  • Shared data races when many threads change it; synchronized gives a lock.
  • wait/notify let threads talk; they need the lock.
  • Deadlock = threads waiting on each other's locks; avoid by consistent lock order.
  • Real projects use Executors + Callable instead of raw threads.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1