FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 3/J3Lesson4/WaitNotifyClass.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Java 3
/
J3Lesson4
/
WaitNotifyClass.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (45 loc) · 1.32 KB
Breadcrumbs
JavaExercises
/
Java 3
/
J3Lesson4
/
WaitNotifyClass.java
Copy path
File metadata and controls
48 lines (45 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public
class
WaitNotifyClass
{
private
final
Object
monitor
=
new
Object
();
private
volatile
char
currentLetter
=
'A'
;
public
static
void
main
(
String
[]
args
) {
WaitNotifyClass
w
=
new
WaitNotifyClass
();
Thread
t1
=
new
Thread
(() -> {
w
.
printA
();
});
Thread
t2
=
new
Thread
(() -> {
w
.
printB
();
});
t1
.
start
();
t2
.
start
();
}
void
printA
() {
synchronized
(
monitor
) {
try
{
for
(
int
i
=
0
;
i
<
3
;
i
++) {
while
(
currentLetter
!=
'A'
)
monitor
.
wait
();
System
.
out
.
print
(
"A"
);
currentLetter
=
'B'
;
monitor
.
notify
();
}
}
catch
(
InterruptedException
ex
) {
ex
.
printStackTrace
();
}
}
}
void
printB
() {
synchronized
(
monitor
) {
try
{
for
(
int
i
=
0
;
i
<
3
;
i
++) {
while
(
currentLetter
!=
'B'
)
monitor
.
wait
();
System
.
out
.
print
(
"B"
);
currentLetter
=
'A'
;
monitor
.
notify
();
}
}
catch
(
InterruptedException
ex
) {
ex
.
printStackTrace
();
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL