3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define multithreading in Java.
- Create and run threads.
- Extend the Thread class.
- Implement the Runnable interface.
- Apply thread synchronization concepts.
3.2 Overview
Multithreading allows Java applications to execute multiple tasks simultaneously. A thread is a lightweight process that runs independently inside a program.
Multithreading is important in:
- gaming applications,
- web servers,
- banking systems,
- background processing,
- and enterprise software.
This lesson introduces learners to practical thread creation and concurrent execution in Java.
Understanding multithreading is important because modern applications perform multiple operations at the same time.
PA1901 — Define Threads
A thread is an independent execution path inside a program.
Thread Benefits
- Faster execution
- Background processing
- Improved performance
- Concurrent task execution
Practical Activity
Learners must:
- define threads,
- explain multithreading,
- and identify thread applications.
PA1902 — Create Threads by Extending Thread Class
Java threads can be created by extending the Thread class.
Java Example:
class MyThread extends Thread {
public void run() {
System.out.println(“Thread Running”);
}
}
Practical Activity
Learners must:
- create thread classes,
- override the run() method,
- and save source files correctly.
PA1903 — Start Threads
Threads execute using the start() method.
Java Example:
MyThread thread = new MyThread();
thread.start();
Practical Activity
Learners must:
- create thread objects,
- start threads,
- and display thread output.
PA1904 — Create Threads Using Runnable Interface
Threads may also implement the Runnable interface.
Java Example:
class MyRunnable implements Runnable {
public void run() {
System.out.println(“Runnable Thread”);
}
}
Practical Activity
Learners must:
- implement Runnable,
- override run(),
- and execute threads.
PA1905 — Execute Runnable Threads
Runnable objects execute through Thread objects.
Java Example:
MyRunnable task = new MyRunnable();
Thread thread = new Thread(task);
thread.start();
Practical Activity
Learners must:
- create Runnable objects,
- execute threads,
- and display outputs.
PA1906 — Use Thread Sleep
Thread sleep pauses execution temporarily.
Java Example:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(e);
}
Practical Activity
Learners must:
- apply thread sleep,
- pause thread execution,
- and handle exceptions.
PA1907 — Synchronize Threads
Synchronization controls shared resource access.
Java Example:
synchronized void display() {
System.out.println(“Synchronized”);
}
Practical Activity
Learners must:
- create synchronized methods,
- manage shared resources,
- and test concurrent execution.
PA1908 — Create Complete Multithreading Program
Multithreading is used in practical Java applications.
Java Example:
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
public class ThreadExample {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
Importance of Multithreading
Multithreading supports:
- concurrent execution,
- improved performance,
- responsive applications,
- and efficient resource utilization.
3.5 Key Notes / Summary
- Multithreading allows simultaneous task execution.
- Threads can extend the Thread class.
- Runnable provides an alternative threading approach.
- start() begins thread execution.
- sleep() pauses thread execution.
- Synchronization controls shared resources.
- Multithreading improves software performance.