-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge9.java
More file actions
80 lines (65 loc) · 1.86 KB
/
Challenge9.java
File metadata and controls
80 lines (65 loc) · 1.86 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
72
73
74
75
76
77
78
79
80
package com.company;
public class Challenge9 {
public static void main(String[] args) {
final NewTutor tutor = new NewTutor();
final NewStudent student = new NewStudent(tutor);
tutor.setStudent(student);
Thread tutorThread = new Thread(new Runnable() {
@Override
public void run() {
tutor.studyTime();
}
});
Thread studentThread = new Thread(new Runnable() {
@Override
public void run() {
student.handInAssignment();
}
});
tutorThread.start();
studentThread.start();
}
}
class NewTutor {
private NewStudent student;
public void setStudent(NewStudent student) {
this.student = student;
}
public void studyTime() {
synchronized (this) {
System.out.println("Tutor has arrived");
synchronized (student) {
try {
// wait for student to arrive
this.wait();
} catch (InterruptedException e) {
}
student.startStudy();
System.out.println("Tutor is studying with student");
}
}
}
public void getProgressReport() {
// get progress report
System.out.println("Tutor gave progress report");
}
}
class NewStudent {
private final NewTutor tutor;
NewStudent(NewTutor tutor) {
this.tutor = tutor;
}
public void startStudy() {
// study
System.out.println("Student is studying");
}
public void handInAssignment() {
synchronized (tutor) {
tutor.getProgressReport();
synchronized (this) {
System.out.println("Student handed in assignment");
tutor.notifyAll();
}
}
}
}