-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.java
80 lines (71 loc) · 1.58 KB
/
Reader.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
package question_6_pro;
import javax.swing.DefaultListModel;
import javax.swing.SwingUtilities;
public class Reader implements Runnable {
private String name = "";
private int time;
private R_W rw = null;
public Reader(R_W rw, int time) {
this.rw = rw;
this.time = time;
}
public void addList(DefaultListModel<String> model) {
model.add(model.getSize(), name + "(读)");
}
public void delList(DefaultListModel<String> model) {
SwingUtilities.invokeLater(() -> {
for (int i = 0; i < model.getSize(); i++) {
if (model.get(i).equals(name + "(读)")) {
model.remove(i);
return ;
}
}
});
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
// TODO 自动生成的方法存根
R_W.Reader_wait.P();
R_W.Rsem.P();
R_W.Rmutex.P();
R_W.read_count++;
if (R_W.read_count == 1) {
R_W.Wsem.P();
}
R_W.Rmutex.V();
R_W.Rsem.V();
R_W.Reader_wait.V();
delList(rw.ui.rmodel);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
addList(rw.ui.omodel);
System.out.println("I am " + name + ", I am reading.");
try {
Thread.sleep(this.time * 1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
R_W.Rmutex.P();
R_W.read_count--;
delList(rw.ui.omodel);
if (R_W.read_count == 0) {
R_W.Wsem.V();
}
R_W.Rmutex.V();
try {
int sec = (int)(Math.random() * 10000);
Thread.sleep(sec);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}