-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheg_SRS.java
More file actions
71 lines (69 loc) · 2.09 KB
/
eg_SRS.java
File metadata and controls
71 lines (69 loc) · 2.09 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
//Using suspend, resume and stop methods or operations
class sus_res_stop implements Runnable {
Thread Th;
boolean suspend_flag, stop_flag;
sus_res_stop(String tN) {
Th = new Thread(this,tN);
suspend_flag = false;
stop_flag = false;
Th.start();
}
public void run() {
try {
int j = 1;
while (++j < 20) {
synchronized (this) {
while(suspend_flag) {
wait();
}
if(stop_flag) {
break;
}
}
}
}
catch (InterruptedException IE) {
System.out.println("Thread Interrupt");
}
}
synchronized void my_suspend() {
suspend_flag = false;
notify();
}
synchronized void my_resume() {
suspend_flag = false;
stop_flag = true;
notify();
}
synchronized void my_stop() {
suspend_flag = false;
stop_flag = true;
notify();
}
}
public class eg_SRS {
public static void main(String[] args) {
try {
sus_res_stop S_R_S_T = new sus_res_stop("SRS");
System.out.println("Thread S_R_S_T is created and started");
Thread.sleep(2000);
S_R_S_T.my_suspend();
System.out.println("Thread S_R_S_T is suspended");
Thread.sleep(2000);
S_R_S_T.my_resume();
System.out.println("Thread S_R_S_T is resumed");
Thread.sleep(2000);
S_R_S_T.my_suspend();
System.out.println("Thread S_R_S_T is suspended");
Thread.sleep(2000);
S_R_S_T.my_resume();
System.out.println("Thread S_R_S_T is resumed");
Thread.sleep(2000);
S_R_S_T.my_stop();
System.out.println("Thread S_R_S_T is stopped");
}
catch (InterruptedException IE) {
System.out.println("Generated interrupted Exception");
}
}
}