-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadOrderRun4.java
More file actions
302 lines (261 loc) · 8 KB
/
Copy pathThreadOrderRun4.java
File metadata and controls
302 lines (261 loc) · 8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package thread;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
/**
* @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 ThreadOrderRun4 {
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;
volatile int fileAid = 1;
volatile int fileBid = 2;
volatile int fileCid = 3;
volatile int fileDid = 4;
CountDownLatch start = new CountDownLatch(1);
public ThreadOrderRun4() 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 {
start.await();
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();
// }
while (fileAid != 1) {
}
synchronized (fileA) {
fileWriter("fileA", str);
fileAid = 2;
}
while (fileDid != 1) {
}
synchronized (fileD) {
fileWriter("fileD", str);
fileDid = 2;
}
while (fileCid != 1) {
}
synchronized (fileC) {
fileWriter("fileC", str);
fileCid = 2;
}
while (fileBid != 1) {
}
synchronized (fileB) {
fileWriter("fileB", str);
fileBid = 2;
}
}
}
private void secondRun(String str) throws IOException, InterruptedException {
start.await();
while (true) {
while (fileBid != 2) {
}
synchronized (fileB) {
fileWriter("fileB", str);
fileBid = 3;
}
while (fileAid != 2) {
}
synchronized (fileA) {
fileWriter("fileA", str);
fileAid = 3;
}
while (fileDid != 2) {
}
synchronized (fileD) {
fileWriter("fileD", str);
fileDid = 3;
}
while (fileCid != 2) {
}
synchronized (fileC) {
fileWriter("fileC", str);
fileCid = 3;
}
}
}
private void thirdRun(String str) throws IOException, InterruptedException {
start.await();
while (true) {
while (fileCid != 3) {
}
synchronized (fileC) {
fileWriter("fileC", str);
fileCid = 4;
}
while (fileBid != 3) {
}
synchronized (fileB) {
fileWriter("fileB", str);
fileBid = 4;
}
while (fileAid != 3) {
}
synchronized (fileA) {
fileWriter("fileA", str);
fileAid = 4;
}
while (fileDid != 3) {
}
synchronized (fileD) {
fileWriter("fileD", str);
fileDid = 4;
}
}
}
private void fourthRun(String str) throws IOException, InterruptedException {
start.await();
while (true) {
while (fileDid != 4) {
}
synchronized (fileD) {
fileWriter("fileD", str);
fileDid = 1;
}
while (fileCid != 4) {
}
synchronized (fileC) {
fileWriter("fileC", str);
fileCid = 1;
}
while (fileBid != 4) {
}
synchronized (fileB) {
fileWriter("fileB", str);
fileBid = 1;
}
while (fileAid != 4) {
}
synchronized (fileA) {
fileWriter("fileA", str);
fileAid = 1;
}
}
}
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 {
ThreadOrderRun4 threadOrderRun4 = new ThreadOrderRun4();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun4.firstRun("1 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun4.secondRun("2 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun4.thirdRun("3 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun4.fourthRun("4 ");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
threadOrderRun4.start.countDown();
}
}