-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadOrderRun2.java
More file actions
274 lines (257 loc) · 8.28 KB
/
Copy pathThreadOrderRun2.java
File metadata and controls
274 lines (257 loc) · 8.28 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
package thread;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @Author: wei1
* @Date: Create in 2018/10/11 20:47
* @Description: 使得多线程按顺序执行
* 思路:使用join
*/
public class ThreadOrderRun2 {
Lock lock1 = new ReentrantLock();
Lock lock2 = new ReentrantLock();
Lock lock3 = new ReentrantLock();
Lock lock4 = new ReentrantLock();
String[] fileNames = {"A", "B", "C", "D"};
volatile boolean[] fileCanWrite = {true, true, true, true};
Condition condition1 = lock1.newCondition();
Condition condition2 = lock1.newCondition();
Condition condition3 = lock1.newCondition();
Condition condition4 = lock1.newCondition();
// Condition condition2 = lock2.newCondition();
// Condition condition3 = lock3.newCondition();
// Condition condition4 = lock4.newCondition();
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 ThreadOrderRun2() 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);
}
@Test
public void test1() throws IOException {
fileA.createNewFile();
}
volatile int needFileId1 = 0;
volatile int needFileId2 = 1;
volatile int needFileId3 = 2;
volatile int needFileId4 = 3;
private void firstRun() {
while (true) {
lock1.lock();
try {
if (!fileCanWrite[needFileId1]) {
condition1.await();
}
fileCanWrite[needFileId1] = !fileCanWrite[needFileId1];
fileWriter("file" + fileNames[needFileId1], "1 ");
int formerNeed = needFileId1;
needFileId1 = (needFileId1 + 1) % 4;
fileCanWrite[formerNeed] = !fileCanWrite[formerNeed];
if (needFileId2 == formerNeed) {
condition2.signal();
}
if (needFileId3 == formerNeed) {
condition3.signal();
}
if (needFileId4 == formerNeed) {
condition4.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
lock1.unlock();
}
}
}
private void secondRun() {
while (true) {
// lock2.lock();
lock1.lock();
try {
if (!fileCanWrite[needFileId2]) {
condition2.await();
}
fileCanWrite[needFileId2] = !fileCanWrite[needFileId2];
fileWriter("file" + fileNames[needFileId2], "2 ");
int formerNeed = needFileId2;
needFileId2 = (needFileId2 + 1) % 4;
fileCanWrite[formerNeed] = !fileCanWrite[formerNeed];
if (needFileId1 == formerNeed) {
condition1.signal();
}
if (needFileId3 == formerNeed) {
condition3.signal();
}
if (needFileId4 == formerNeed) {
condition4.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
lock1.unlock();
}
}
}
private void thirdRun() {
while (true) {
// lock3.lock();
lock1.lock();
try {
if (!fileCanWrite[needFileId3]) {
condition3.await();
}
fileCanWrite[needFileId3] = !fileCanWrite[needFileId3];
fileWriter("file" + fileNames[needFileId3], "3 ");
int formerNeed = needFileId3;
needFileId3 = (needFileId3 + 1) % 4;
fileCanWrite[formerNeed] = !fileCanWrite[formerNeed];
if (needFileId1 == formerNeed) {
condition1.signal();
}
if (needFileId2 == formerNeed) {
condition2.signal();
}
if (needFileId4 == formerNeed) {
condition4.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// lock3.unlock();
lock1.unlock();
}
}
}
private void fourthRun() {
while (true) {
// lock4.lock();
lock1.lock();
try {
if (!fileCanWrite[needFileId4]) {
condition4.await();
}
fileCanWrite[needFileId4] = !fileCanWrite[needFileId4];
fileWriter("file" + fileNames[needFileId4], "4 ");
int formerNeed = needFileId4;
needFileId4 = (needFileId4 + 1) % 4;
fileCanWrite[formerNeed] = !fileCanWrite[formerNeed];
if (needFileId1 == formerNeed) {
condition1.signal();
}
if (needFileId2 == formerNeed) {
condition2.signal();
}
if (needFileId3 == formerNeed) {
condition3.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
lock1.unlock();
}
}
}
private void fileWriter(String fileName, String ch) throws IOException {
if (fileName == null) {
return;
}
if (fileName.equals("fileA")) {
fwA.write(ch);
fwA.flush();
}
if (fileName.equals("fileB")) {
fwB.write(ch);
fwB.flush();
}
if (fileName.equals("fileC")) {
fwC.write(ch);
fwC.flush();
}
if (fileName.equals("fileD")) {
fwD.write(ch);
fwD.flush();
}
}
// @Test
// public void testOrder() {
// new Thread(new Runnable() {
// @Override
// public void run() {
// firstRun(5);
// }
// }, "thread---1").start();
//
// new Thread(new Runnable() {
// @Override
// public void run() {
// secondRun(5);
// }
// }, "thread---2").start();
//
// new Thread(new Runnable() {
// @Override
// public void run() {
// thirdRun(5);
// }
// }, "thread---3").start();
// }
public static void main(String[] args) throws IOException {
ThreadOrderRun2 threadOrderRun2 = new ThreadOrderRun2();
new Thread(new Runnable() {
@Override
public void run() {
threadOrderRun2.firstRun();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
threadOrderRun2.secondRun();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
threadOrderRun2.thirdRun();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
threadOrderRun2.fourthRun();
}
}).start();
}
}