-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrency.java
More file actions
130 lines (110 loc) · 4.22 KB
/
Copy pathConcurrency.java
File metadata and controls
130 lines (110 loc) · 4.22 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
package concurrency;
import com.github.javafaker.Faker;
import concurrency.entity.Package;
import concurrency.entity.PackageEnums;
import concurrency.entity.Snack;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Concurrency {
public static final File files = new File("src/resource/file/concurrency");
public static final String OUTPUT_FILE_SOURCE = "src/resource/file/concurrency";
public static final Faker faker = new Faker();
public static void main(String[] args) {
Package aPackage = createPackage();
int totalPackage = 50;
aPackage.setTotalSnack(totalPackage);
ExecutorService executorService = Executors.newFixedThreadPool(50);
for (int i = 1; i <= totalPackage; i++) {
Thread thread = new Thread(() -> {
Snack snack = createSnack();
snack.setStatus(true);
aPackage.getSnack().add(snack);
write("src/resource/file/concurrency" + "/" + aPackage.getName() + "/" + snack.getSnackName() + ".txt", snack.toString());
for (int i1 = 0; i1 < 10000; i1++) {
System.out.println("Thread " + snack.getSnackName() + ":" + "Processing " + i1);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
setDone(aPackage);
});
executorService.execute(thread);
}
executorService.shutdown();
}
public static void setDone(Package aPackage) {
boolean isTrue = aPackage.getSnack().stream().allMatch(Snack::isStatus);
if (!files.exists()) {
return;
}
System.out.println("File Length: "+ countFileSize(aPackage.getName()));
if (
isTrue
&& countFileSize(aPackage.getName()) == aPackage.getTotalSnack()
) {
aPackage.setStatus(PackageEnums.DONE.getStatusName());
System.out.println(aPackage.toStringList());
}
}
public static int countFileSize(String folder){
File file = new File(OUTPUT_FILE_SOURCE + "/" + folder);
return file.listFiles().length;
}
public static void write(String filePath, String bodyFile) {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
byte[] bytes = bodyFile.getBytes();
fos.write(bytes);
} catch (IOException e) {
throw new RuntimeException("Error writing file " + filePath, e);
}
}
public static Package createPackage(Snack snack) {
return Package.builder()
.name("Package 1")
.snack(new ArrayList<>())
.build();
}
public static Package createPackage() {
return Package.builder()
.name(createPackageName())
.snack(new ArrayList<>())
.build();
}
public static String createPackageName() {
StringBuilder packageName = new StringBuilder();
int index = 1;
while (true) {
packageName.append("Package").append("_").append(index);
if (!doFolderExist(packageName.toString())) {
createFolder(packageName.toString());
break;
} else {
packageName.delete(0, packageName.length());
index++;
}
}
return packageName.toString();
}
public static boolean doFolderExist(String folder) {
File file = new File(OUTPUT_FILE_SOURCE + "/" + folder);
return file.exists();
}
public static boolean createFolder(String folder) {
File file = new File(OUTPUT_FILE_SOURCE + "/" + folder);
return file.mkdir();
}
public static Snack createSnack() {
return Snack.builder()
.price(faker.book().hashCode())
.quantity(faker.address().hashCode())
.snackName(faker.book().author())
.status(false)
.build();
}
}