CHAPTER 23
Beginner
Multithreading in Java
Updated: May 17, 2026
5 min read
# CHAPTER 23
Multithreading in Java
1. Introduction
Modern applications must do multiple things simultaneously — downloading a file while updating the UI, serving thousands of web requests at once. Multithreading allows Java programs to execute multiple tasks (threads) concurrently within a single process.2. Creating Threads
Method 1: Extending Thread
java
Method 2: Implementing Runnable (Preferred)
java
Method 3: Lambda (Java 8+)
java
3. Thread Lifecycle
4. Thread Methods
| Method | Description |
|---|---|
start() | Starts the thread |
run() | Contains the code to execute |
sleep(ms) | Pauses for milliseconds |
join() | Waits for thread to finish |
getName() | Gets thread name |
setPriority(n) | Sets priority (1-10) |
isAlive() | Checks if thread is running |
5. Synchronization
When multiple threads access shared data, race conditions can corrupt it:
java
The synchronized keyword ensures only ONE thread can execute the method at a time.
6. Thread Safety with Synchronized Block
java
7. ExecutorService (Modern Approach)
java
8. MCQ Quiz with Answers
Question 1
Which method starts a thread?
Question 2
Runnable interface has which method?
Question 3
synchronized prevents:
Question 4
Thread.sleep() does what?
Question 5
join() makes the calling thread:
Question 6
Which is preferred: Thread class or Runnable?
Question 7
ExecutorService manages:
Question 8
A deadlock occurs when:
Question 9
Thread priority range is:
Question 10