-
Notifications
You must be signed in to change notification settings - Fork 0
/
R_W.java
60 lines (49 loc) · 1.45 KB
/
R_W.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
package question_6_pro;
import java.util.ArrayList;
import java.util.List;
public class R_W {
public List<Reader> reader = new ArrayList<Reader>();
public List<Writer> writer = new ArrayList<Writer>();
static public List<Thread> threads = new ArrayList<Thread>();
static public Semaphore Rmutex = new Semaphore(1);
static public Semaphore Wmutex = new Semaphore(1);
static public Semaphore Rsem = new Semaphore(1);
static public Semaphore Wsem = new Semaphore(1);
static public Semaphore Reader_wait = new Semaphore(1);
static public int read_count = 0;
static public int write_count = 0;
private int r = 0;
private int w = 0;
public UI ui = null;
public void addReader(int num, int time) {
for (int i = 0; i < num; i++) {
Reader rd = new Reader(this, time);
rd.setName("reader" + (i + this.r));
reader.add(rd);
threads.add(new Thread(rd));
this.ui.rmodel.add(this.ui.rmodel.getSize(), "reader" + (i + this.r) + "(¶Á)");
}
this.r += num;
}
public void addWriter(int num, int time) {
for (int i = 0; i < num; i++) {
Writer wt = new Writer(this, time);
wt.setName("writer" + (i + this.w));
writer.add(wt);
threads.add(new Thread(wt));
this.ui.wmodel.add(this.ui.wmodel.getSize(), "writer" + (i + this.w) + "(д)");
}
this.w += num;
}
public R_W(UI ui) {
this.ui = ui;
}
public void test() {
for (Thread thread : threads) {
thread.start();
}
threads.clear();
reader.clear();
writer.clear();
}
}