-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr21Multi.java
89 lines (81 loc) · 1.74 KB
/
pr21Multi.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class ThreadA extends Thread
{
private boolean suspended = false;
public void run()
{
synchronized (this)
{
System.out.println("ThreadA started.");
try
{
while (suspended)
{
wait();
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("ThreadA resumed.");
}
}
public void resumeThread()
{
suspended = false;
synchronized(this)
{
notify();
}
}
}
class ThreadB extends Thread
{
public void run()
{
synchronized (this)
{
System.out.println("ThreadB started.");
try
{
wait();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("ThreadB resumed.");
}
}
}
public class pr21Multi
{
public static void main(String[] args)
{
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB();
threadA.start();
threadB.start();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
threadA.resumeThread();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (threadB)
{
threadB.notify();
}
}
}