Repository navigation
Expand file tree
/
Copy pathTema1.java
More file actions
88 lines (71 loc) · 3.26 KB
/
Copy pathTema1.java
File metadata and controls
88 lines (71 loc) · 3.26 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
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CyclicBarrier;
// Main entry point of the application
// Handles initialization, configuration loading, and thread orchestration
public class Tema1 {
public static void main(String[] args) {
if (args.length < 3) return;
int numThreads = Integer.parseInt(args[0]);
String articlesInputFile = args[1];
String auxInputFile = args[2];
try {
solve(numThreads, articlesInputFile, auxInputFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void solve(int numThreads, String articlesInputFile, String auxInputFile) throws Exception {
// Load all static configuration data before starting parallel execution
setupAuxiliaryData(articlesInputFile, auxInputFile);
// Initialize the synchronization barrier for the fixed number of thread
SharedData.barrier = new CyclicBarrier(numThreads);
Thread[] threads = new Thread[numThreads];
// Create and start the worker thread
for (int i = 0; i < numThreads; i++) {
threads[i] = new Worker(i);
threads[i].start();
}
// Main thread waits for all workers to finis
for (int i = 0; i < numThreads; i++) {
threads[i].join();
}
}
private static void setupAuxiliaryData(String articlesInputFile, String auxInputFile) throws IOException {
File inputsFile = new File(auxInputFile);
File baseDir = inputsFile.getParentFile();
// Read the config file which contains paths to other auxiliary files
List<String> rawAuxLines = Files.readAllLines(inputsFile.toPath());
List<String> al = new ArrayList<>();
for (String line : rawAuxLines) {
String trimmed = line.trim();
if (!trimmed.isEmpty()) {
al.add(trimmed);
}
}
if (al.size() < 4) return;
// Load Languages into a LinkedList (deliberately keeping O(N) lookup for this part)
SharedData.targetLanguages = Utils.loadLinkedList(Utils.resolvePath(baseDir, al.get(1)), true);
// Load Categories into a LinkedList.
SharedData.targetCategories = Utils.loadCategories(Utils.resolvePath(baseDir, al.get(2)));
// Load Stop Words into a HashSet (Optimized to O(1)) to speed up the heavy text processing loop.
SharedData.stopWords = Utils.loadHashSet(Utils.resolvePath(baseDir, al.get(3)), true);
// Read the master file containing the list of JSON files to process.
File artListFile = new File(articlesInputFile);
File artBaseDir = artListFile.getParentFile();
List<String> rawArtLines = Files.readAllLines(artListFile.toPath());
List<String> lines = new ArrayList<>();
for (String line : rawArtLines) {
String trimmed = line.trim();
if (!trimmed.isEmpty()) {
lines.add(trimmed);
}
}
// Populate the shared work queue with file paths.
SharedData.inputJsonFiles = new LinkedList<>();
for (int i = 1; i < lines.size(); i++) {
SharedData.inputJsonFiles.add(Utils.resolvePath(artBaseDir, lines.get(i)));
}
}
}