Java Notes: Multithreading
1. Definition
Multithreading is a Java feature that allows multiple threads to execute concurrently within a program. A
thread is a lightweight unit of execution.
2. What is a Thread?
A thread is an independent path of execution. A Java program can have multiple threads performing
different tasks at the same time.
3. Advantages of Multithreading
• Better CPU utilization
• Improved application responsiveness
• Concurrent execution of tasks
• Efficient use of system resources
• Useful for games, servers, animations and background tasks.
4. Creating a Thread
There are two common ways to create a thread: by extending the Thread class and by implementing the
Runnable interface.
5. Extending Thread Class
A class can extend Thread and override the run() method. The start() method begins thread
execution.
6. Implementing Runnable
A class can implement Runnable and provide the run() method. A Thread object is then created using the
Runnable object.
7. Thread Life Cycle
A thread commonly moves through states such as NEW, RUNNABLE, BLOCKED, WAITING,
TIMED_WAITING and TERMINATED.
8. Important Thread Methods
start() starts a new thread; run() contains the task; sleep() pauses execution for a specified time; join()
waits for another thread to finish; interrupt() requests interruption; isAlive() checks whether a thread is
alive.
9. Thread Priority
Java threads have priorities. The constants MIN_PRIORITY, NORM_PRIORITY and MAX_PRIORITY
represent priorities 1, 5 and 10 respectively. Priority is a scheduling hint and does not guarantee
execution order.
10. Synchronization
Synchronization controls access to shared resources so that multiple threads do not interfere with each
other. The synchronized keyword can be used for methods or blocks.
11. Inter-Thread Communication
Threads can coordinate using wait(), notify(), and notifyAll(). These methods are used with
synchronization and an object's monitor.
12. Common Problems
Race condition occurs when multiple threads access shared data without proper coordination. Deadlock
occurs when threads wait indefinitely for resources held by each other.
13. Example: Creating a Thread
class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}
}
class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
14. Example: Runnable
class Task implements Runnable {
public void run() {
[Link]("Task is running");
}
}
class Main {
public static void main(String[] args) {
Thread t = new Thread(new Task());
[Link]();
}
}
15. synchronized Example
class Counter {
private int count = 0;
synchronized void increment() {
count++;
}
}
Quick Revision
Concept Key Point
Thread Lightweight unit of execution
start() Starts a new thread
run() Contains thread task
sleep() Temporarily pauses a thread
join() Waits for another thread to finish
synchronized Protects shared resources
wait() Makes a thread wait
notify() Wakes one waiting thread
notifyAll() Wakes all waiting threads
Race Condition Incorrect result due to unsynchronized shared access
Deadlock Threads wait indefinitely for each other
Exam Tip: Remember that start() creates a separate thread of execution, while directly calling run() is
just a normal method call.