Module 4
Multithreading & Concurrency in Java
25 mins read
Lesson 1: Introduction to Multithreading
1.1 Understanding Multithreading in Java
Multithreading enables concurrent execution of threads within a process to optimize hardware resources. Java supports threads through the Thread class and Runnable interface.
JAVA
// Creating and starting a thread
class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
1.2 Thread Lifecycle and States
A Java thread transitions through NEW, RUNNABLE, RUNNING, BLOCKED, WAITING, and TERMINATED states during its lifecycle.
JAVA
// Checking thread state
Thread.State state = thread.getState();
System.out.println("Thread state: " + state);