What is Multithreading in Java?
Learn what multithreading in Java is, how threads share memory, Runnable vs Callable, ExecutorService, and key interview questions with clear code examples.
Expected Interview Answer
Multithreading in Java is the ability of a program to run multiple threads concurrently within a single process, where each thread is an independent path of execution that shares the process's memory and resources.
The JVM schedules threads onto available CPU cores, so tasks like handling requests, computing, and I/O can progress in parallel or interleave on a single core. Threads are created by extending Thread, implementing Runnable/Callable, or using an ExecutorService, and they share heap memory while each keeps its own stack and program counter. Because state is shared, correct multithreading requires synchronization to avoid race conditions and visibility bugs.
- Better CPU utilization on multi-core hardware
- Higher throughput for I/O-bound and parallel workloads
- More responsive applications (UI or servers stay reactive)
- Natural modelling of concurrent tasks
- Resource sharing within one process is cheaper than multiple processes
AI Mentor Explanation
A cricket ground runs many things at once during play: the bowler bowls, fielders reposition, the scorer updates the book, and the third umpire reviews replays. Each acts independently yet shares one match state, just as Java threads run separate execution paths while sharing the same process memory to move the game forward together.
Step-by-Step Explanation
Step 1
Understand a thread
A thread is a lightweight unit of execution with its own stack and program counter but shared heap and static memory.
Step 2
Create the thread
Extend Thread or, preferably, implement Runnable/Callable and hand the task to a Thread or ExecutorService.
Step 3
Start execution
Call start() (never run() directly) so the JVM allocates a new thread and invokes run() on it.
Step 4
Let the scheduler run threads
The OS/JVM scheduler time-slices runnable threads across available cores, interleaving or parallelizing them.
Step 5
Coordinate shared state
Use synchronized, locks, volatile, or concurrent collections to prevent race conditions and visibility bugs.
Step 6
Join or shut down
Use join() to wait for completion, or shutdown() an ExecutorService to release the thread pool cleanly.
What Interviewer Expects
- Clear definition of a thread versus a process
- Knowledge of Runnable, Callable, Thread and ExecutorService
- Awareness that threads share heap memory but keep separate stacks
- Understanding why synchronization is needed for shared state
- Difference between concurrency and parallelism
Common Mistakes
- Calling run() instead of start(), which runs on the same thread
- Assuming multithreading always makes code faster
- Ignoring race conditions and visibility issues on shared data
- Creating threads manually instead of using a thread pool
- Confusing concurrency (structure) with parallelism (simultaneous execution)
Best Answer (HR Friendly)
“Multithreading lets one Java program do several things at the same time by running independent tasks called threads. It uses the computer's cores more fully and keeps apps responsive, but the threads share memory, so the code must be written carefully to avoid conflicts.”
Code Example
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultithreadingDemo {
public static void main(String[] args) throws InterruptedException {
// 1) Manual thread using a Runnable
Runnable task = () -> {
String name = Thread.currentThread().getName();
System.out.println("Running on: " + name);
};
Thread t = new Thread(task, "worker-1");
t.start(); // start(), NOT run()
t.join(); // wait for it to finish
// 2) Preferred: a pool of reusable threads
ExecutorService pool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
int id = i;
pool.submit(() -> System.out.println("Task " + id
+ " on " + Thread.currentThread().getName()));
}
pool.shutdown();
}
}Follow-up Questions
- What is the difference between a process and a thread?
- How do concurrency and parallelism differ?
- Why prefer ExecutorService over creating threads manually?
- What is the difference between Runnable and Callable?
- What problems can arise from sharing mutable state across threads?
MCQ Practice
1. Which method actually starts a new thread of execution?
start() asks the JVM to create a new thread and then invoke run() on it. Calling run() directly executes on the current thread with no concurrency.
2. What do threads within the same process share?
Threads share the process heap and static/method-area memory but each has its own stack and program counter.
3. Which interface is best when a task must return a result or throw a checked exception?
Callable<V> returns a value and can throw checked exceptions; Runnable returns void and cannot throw checked exceptions.
Flash Cards
What is a thread? — A lightweight, independent path of execution within a process, with its own stack but shared heap memory.
start() vs run()? — start() spawns a new thread and calls run() on it; calling run() directly executes on the current thread.
Runnable vs Callable? — Runnable.run() returns void and cannot throw checked exceptions; Callable.call() returns a value and can.
Concurrency vs parallelism? — Concurrency is structuring work as independent tasks; parallelism is running them literally at the same time on multiple cores.