-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultithreading.java
70 lines (60 loc) · 2.9 KB
/
Multithreading.java
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
class A extends Thread {
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println("From A: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Exit From A");
}
}
class B extends Thread {
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println("From B: " + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Exit From B");
}
}
public class Multithreading {
public static void main(String[] args) {
A threadA = new A();
B threadB = new B();
threadA.start();
threadB.start();
}
}
/*
* Approach:
The problem involves implementing multithreading in Java using two separate classes A and B that extend the Thread class.
Preface:
The program defines two classes, A and B, both of which extend the Thread class. Each class overrides the run() method to perform some tasks concurrently. In the main() method of the Multithreading class, instances of A and B are created, and their start() methods are called to begin their execution concurrently.
Logic:
Class A defines a thread that prints messages "From A: " followed by the current iteration number from 0 to 3. It then sleeps for 100 milliseconds between iterations.
Class B defines a similar thread that prints messages "From B: " followed by the current iteration number from 0 to 3. It then sleeps for 200 milliseconds between iterations.
In the main() method of the Multithreading class:
Instances of A and B threads, named threadA and threadB respectively, are created.
The start() method is called on both threadA and threadB, which begins the execution of their respective run() methods concurrently.
Pseudocode:
Define class A extending Thread:
a. Override the run() method to print messages "From A: " followed by the current iteration number from 0 to 3 and sleep for 100 milliseconds between iterations.
Define class B extending Thread:
a. Override the run() method to print messages "From B: " followed by the current iteration number from 0 to 3 and sleep for 200 milliseconds between iterations.
In the main method of class Multithreading:
a. Create an instance of class A named threadA.
b. Create an instance of class B named threadB.
c. Start the execution of threadA.
d. Start the execution of threadB.
Time Complexity:
Creating and starting threads: O(1)
Output:
The program will produce output showing interleaved messages from both threads A and B executing concurrently. Each thread iterates from 0 to 3 and prints a message along with a delay between each iteration. Additionally, each thread will print "Exit From A" or "Exit From B" after completing its iterations.
*/