-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadOrderRun6.java
More file actions
152 lines (136 loc) · 5.16 KB
/
Copy pathThreadOrderRun6.java
File metadata and controls
152 lines (136 loc) · 5.16 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
package thread;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
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: 有四个线程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 ThreadOrderRun6 {
HashMap<Character, FileWriter> fwhm = new HashMap<>();
HashMap<Character, Lock> lockhm = new HashMap<>();
HashMap<Character, AtomicInteger> fidhm = new HashMap<>();
HashMap<String, Condition> chm = new HashMap<>();
CountDownLatch start = new CountDownLatch(1);
public ThreadOrderRun6(char[] chs) throws IOException {
//初始化创建文件A B C D
int x = 0;
for (char ch : chs) {
File file = new File(ch + ".txt");
if (file != null) {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
fwhm.put(ch, new FileWriter(file));
lockhm.put(ch, new ReentrantLock());
if (x == 0) {
fidhm.put(ch, new AtomicInteger(1));
x++;
} else {
fidhm.put(ch, new AtomicInteger(chs.length + 1 - x));
x++;
}
}
for (char ch : chs) {
for (int j = 1; j <= chs.length; j++) {
chm.put("" + ch + j, lockhm.get(ch).newCondition());
}
}
}
private void firstRun() throws IOException, InterruptedException {
String threadName = Thread.currentThread().getName();
System.out.println(threadName);
String[] strs = threadName.split("_");
char[] chs = strs[0].toCharArray();
int i = Integer.parseInt(strs[1]) + 1;
start.await();
while (true) {
for (int j = 0; j < size; j++) {
Lock lock = lockhm.get(chs[j]);
lock.lock();
try {
while (fidhm.get(chs[j]).get() != i) {
chm.get("" + chs[j] + i).await();
}
fileWriter(chs[j], i + " ");
if (i == size) {
fidhm.get(chs[j]).getAndSet(1);
} else {
fidhm.get(chs[j]).getAndSet(i + 1);
}
// System.out.println("signal:" + chs[j] + fidhm.get(chs[j]).get());
chm.get("" + chs[j] + fidhm.get(chs[j]).get()).signal();
} finally {
lock.unlock();
}
}
}
}
private void fileWriter(char fileName, String str) throws IOException {
fwhm.get(fileName).write(str);
fwhm.get(fileName).flush();
}
public static int size = 4;
public static void main(String[] args) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
char start = 'A';
stringBuilder.append(start);
for (int i = 1; i < size; i++) {
stringBuilder.append((char) (start + size - i));
}
String order = stringBuilder.toString();
ThreadOrderRun6 threadOrderRun6 = new ThreadOrderRun6(order.toCharArray());
for (int i = 0; i < size; i++) {
String sub1 = order.substring(0, size - i);
String sub2 = order.substring(size - i, size);
String threadName = sub2 + sub1 + "_" + i;
new Thread(new Runnable() {
@Override
public void run() {
try {
threadOrderRun6.firstRun();
} catch (Exception e) {
e.printStackTrace();
}
}
}, threadName).start();
}
threadOrderRun6.start.countDown();
}
@Test
public void test() {
String str = "ABCD";
for (int i = 0; i < str.length(); i++) {
String sub1 = str.substring(0, i);
String sub2 = str.substring(i, str.length());
System.out.println(sub1 + " " + sub2);
}
}
}