forked from kabirthakkar/Hkfst2k21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threading.java
62 lines (49 loc) · 1.21 KB
/
Threading.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
import java.util.*;
class ThreadA extends Thread {
private String name;
ThreadA(){}
ThreadA(String obj) { // overrid. constructor
obj = name;
}
@Override
public void run() {
while (true) {
System.out.println("Thread A");
try {
ThreadA.sleep(1500);
}
catch (InterruptedException e) {
System.out.println("interrupted A");
}
}
}
}
class ThreadB extends Thread {
@Override
public void run() {
while(true) {
System.out.println("Thread B");
try {
ThreadB.sleep(1000);
} catch (InterruptedException e) {
System.out.println("interrupted B");
}
}
}
}
public class Threading {
public static void main(String[] args) {
var a = new ThreadA();
var b = new ThreadB();
a.start();
b.start();
var sysIn = new Scanner(System.in);
String obj;
while ((obj = sysIn.next()) != "x");
sysIn.close();
if ("x".equals(obj)) {
a.interrupt();
b.interrupt();
}
}
}