Lesson 40
Multithreading
Multithreading runs multiple threads concurrently. Improves performance and CPU use.
Java Track
Save progress as you learn.
61 lessons
Lesson content
Multithreading runs tasks concurrently using threads.
Key Terms
- Thread: Smallest execution unit in a process.
- Multithreading: Multiple threads running concurrently.
- Runnable: Interface defining a thread task.
- Synchronization: Controls shared resource access.
Two Ways to Create Threads
- Extend the Thread class.
- Implement the Runnable interface.
Example 1: Thread Class
class A extends Thread {
public void run() {
for (int i = 1; i <= 5; i++)
System.out.println("Welcome to C");
}
}
class B extends Thread {
public void run() {
for (int i = 1; i <= 5; i++)
System.out.println("Welcome to C++");
}
}
class C extends Thread {
public void run() {
for (int i = 1; i <= 5; i++)
System.out.println("Welcome to Java");
}
}
class threaddemo {
public static void main(String args[]) {
A a = new A();
a.start();
B b = new B();
b.start();
C c = new C();
c.start();
}
}Example 2: Runnable Interface
package com.java.Multi_threading;
class Task1 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 1 - Counting: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Task2 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 2 - Counting: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadDemo {
public static void main(String[] args) {
Task1 task1 = new Task1();
Task2 task2 = new Task2();
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
thread1.start();
thread2.start();
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread - Counting: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}Example 3: Shared Resource
package com.java.Multi_threading;
class Table {
void printTable(int n) {
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) { this.t = t; }
public void run() { t.printTable(5); }
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) { this.t = t; }
public void run() { t.printTable(100); }
}
class TestSynchronization1 {
public static void main(String args[]) {
Table obj = new Table();
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}Thread Lifecycle
- New — Thread object created.
- Runnable — Ready to run.
- Running — Executing.
- Blocked/Waiting — Waiting for resource.
- Terminated — Execution complete.
FAQ
1. What is multithreading?
Multiple threads executing concurrently in one program.
2. Thread vs Runnable?
Thread is a class. Runnable is an interface.
3. Why synchronization?
Prevents data corruption from concurrent access.
Threads are lightweight sub-processes.
Extend Thread or implement Runnable.
Call start(), not run() directly.
Runnable preferred — allows inheritance.
Thread.sleep() pauses execution temporarily.
Synchronize shared resources always.
Too many threads reduces performance.
Where Java is used
Java in real-world development
Thread Class
Extend Thread. Override run(). Call start() to begin.
Runnable Interface
Implement Runnable. Pass to Thread object. Call start().
start() vs run()
start() creates new thread. run() executes in current thread.
Thread.sleep()
Pauses thread for milliseconds. Throws InterruptedException.
Synchronization
Prevents concurrent access issues. Use synchronized keyword.
Thread Lifecycle
New → Runnable → Running → Blocked → Terminated.