Multithread in Java
What is a Thread?
A thread is the smallest unit of a process that can be scheduled for execution. It is a lightweight process that shares the same memory and resources of its parent process, allowing multiple tasks to be performed simultaneously. Java provides built-in support for multithreaded programming, which allows applications to perform multiple tasks at the same time, improving performance and responsiveness.
Video Explanation

Key Concepts
- Process: An independent program in execution, with its own memory space.
- Thread: A subset of a process that shares the process’s memory and resources.
- Multithreading: The ability to execute multiple threads simultaneously.
Why Use Multithreading?
Multithreading enables efficient utilization of the CPU and improves the performance of applications by:
- Running tasks concurrently.
- Reducing idle time by utilizing CPU cycles better.
- Enhancing responsiveness in applications, especially in UI-based applications.
Creating a Thread in Java
Java provides two primary ways to create a thread:
- Extending the Thread class
- Implementing the Runnable interface
1. Extending the Thread Class
To create a thread by extending the Thread class, a class must inherit from Thread and override its run() method.
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
}
}
2. Implementing the Runnable Interface
Another way to create a thread is by implementing the Runnable interface, which has a single run() method.
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start(); // Starts the thread and calls the run() method
}
}
Note: Implementing Runnable is preferred over extending Thread when a class already extends another class, as Java does not support multiple inheritance.