-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultithreading_Demo.java
More file actions
75 lines (67 loc) · 2.27 KB
/
Multithreading_Demo.java
File metadata and controls
75 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Program: Multithreading Demo
* Description: A Java program to demonstrate the concept of Multithreading by extending the Thread class.
* It creates multiple threads that run concurrently.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
class ThreadOne extends Thread {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread 1 (A) -- Value: " + i);
try {
// Adding a small delay to visualize concurrency
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class ThreadTwo extends Thread {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread 2 (B) -- Value: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class ThreadThree extends Thread {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread 3 (C) -- Value: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class Multithreading_Demo {
public static void main(String[] args) {
System.out.println("---------------------------------------------");
System.out.println(" Multithreading Demo");
System.out.println("---------------------------------------------");
// Creating thread objects
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
ThreadThree t3 = new ThreadThree();
System.out.println("Status of Thread 1 before start: " + t1.isAlive());
// Starting threads
System.out.println("Starting threads...");
t1.start();
t2.start();
t3.start();
System.out.println("Status of Thread 1 after start: " + t1.isAlive());
System.out.println("\nAll threads started. They will execute concurrently.");
System.out.println("---------------------------------------------");
}
}