-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadOrderRun3.java
More file actions
235 lines (218 loc) · 6.89 KB
/
Copy pathThreadOrderRun3.java
File metadata and controls
235 lines (218 loc) · 6.89 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package thread;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Author: wei1
* @Date: Create in 2018/10/11 20:47
* @Description: 有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推。
* 现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:
* A. 1 2 3 4 1 2....
* B. 2 3 4 1 2 3....
* C. 3 4 1 2 3 4....
* D. 4 1 2 3 4 1....
* 请设计程序。
* 4个线程同时执行,每个负责向文件输出1 2 3 4
* 就是线程1向A B C D 四个文件输出1
* 就是线程2向A B C D 四个文件输出2
* 就是线程3向A B C D 四个文件输出3
* 就是线程4向A B C D 四个文件输出4
* 开始就是用Reentrant的一个锁对应多个condition来控制流程,但是这样是一个线程在写,3个在等没有意义
* 现在发现这个是有执行顺序的,注意每个IO完成的时间都不一样,注意sleep一会
* 线程1: A -> D -> C -> B
* 线程2: B -> A -> D -> C
* 线程3: C -> B -> A -> D
* 线程4: D -> C -> B -> A
*/
public class ThreadOrderRun3 {
File fileA = new File("A.txt");
File fileB = new File("B.txt");
File fileC = new File("C.txt");
File fileD = new File("D.txt");
FileWriter fwA;
FileWriter fwB;
FileWriter fwC;
FileWriter fwD;
public ThreadOrderRun3() throws IOException {
//初始化创建文件A B C D
if (fileA.exists() || fileB.exists() || fileC.exists() || fileD.exists()) {
fileA.delete();
fileB.delete();
fileC.delete();
fileD.delete();
}
fileA.createNewFile();
fileB.createNewFile();
fileC.createNewFile();
fileD.createNewFile();
fwA = new FileWriter(fileA);
fwB = new FileWriter(fileB);
fwC = new FileWriter(fileC);
fwD = new FileWriter(fileD);
}
private void firstRun(String str) throws IOException, InterruptedException {
while (true) {
// try {
// lock1.lock();
// if (lock1id != 1) {
// condition11.await();
// }
// fileWriter("fileA", "1 ");
// lock1id = 2;
// condition12.signal();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// lock1.unlock();
// }
synchronized (fileA) {
fileWriter("fileA", str);
}
Thread.sleep(50);
synchronized (fileD) {
fileWriter("fileD", str);
}
Thread.sleep(50);
synchronized (fileC) {
fileWriter("fileC", str);
}
Thread.sleep(50);
synchronized (fileB) {
fileWriter("fileB", str);
}
Thread.sleep(50);
}
}
private void secondRun(String str) throws IOException, InterruptedException {
while (true) {
synchronized (fileB) {
fileWriter("fileB", str);
}
Thread.sleep(50);
synchronized (fileA) {
fileWriter("fileA", str);
}
Thread.sleep(50);
synchronized (fileD) {
fileWriter("fileD", str);
}
Thread.sleep(50);
synchronized (fileC) {
fileWriter("fileC", str);
}
Thread.sleep(50);
}
}
private void thirdRun(String str) throws IOException, InterruptedException {
while (true) {
synchronized (fileC) {
fileWriter("fileC", str);
}
Thread.sleep(50);
synchronized (fileB) {
fileWriter("fileB", str);
}
Thread.sleep(50);
synchronized (fileA) {
fileWriter("fileA", str);
}
Thread.sleep(50);
synchronized (fileD) {
fileWriter("fileD", str);
}
Thread.sleep(50);
}
}
private void fourthRun(String str) throws IOException, InterruptedException {
while (true) {
synchronized (fileD) {
fileWriter("fileD", str);
}
Thread.sleep(50);
synchronized (fileC) {
fileWriter("fileC", str);
}
Thread.sleep(50);
synchronized (fileB) {
fileWriter("fileB", str);
}
Thread.sleep(50);
synchronized (fileA) {
fileWriter("fileA", str);
}
Thread.sleep(50);
}
}
private void fileWriter(String fileName, String str) throws IOException {
if (fileName == null) {
return;
}
if (fileName.equals("fileA")) {
fwA.write(str);
fwA.flush();
}
if (fileName.equals("fileB")) {
fwB.write(str);
fwB.flush();
}
if (fileName.equals("fileC")) {
fwC.write(str);
fwC.flush();
}
if (fileName.equals("fileD")) {
fwD.write(str);
fwD.flush();
}
}
public static void main(String[] args) throws IOException {
ThreadOrderRun3 threadOrderRun3 = new ThreadOrderRun3();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun3.firstRun("1 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun3.secondRun("2 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun3.thirdRun("3 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun3.fourthRun("4 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}