-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpratical2.java
More file actions
119 lines (99 loc) · 3.55 KB
/
pratical2.java
File metadata and controls
119 lines (99 loc) · 3.55 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
import java.io.*;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class WordCountTask extends Thread {
private final List<String> lines;
private final SharedResult sharedResult;
public WordCountTask(List<String> lines, SharedResult sharedResult, String name) {
super(name); // set thread name
this.lines = lines;
this.sharedResult = sharedResult;
}
@Override
public void run() {
System.out.println(getName() + " started.");
int localCount = 0;
for (String line : lines) {
String[] words = line.trim().split("\\s+");
localCount += words.length;
// Example: also check for a pattern (e.g., "Java")
for (String word : words) {
if (word.equalsIgnoreCase("Java")) {
sharedResult.addPatternMatch("Java found by " + getName());
}
}
}
// Add result to shared counter safely
sharedResult.addWordCount(localCount);
System.out.println(getName() + " finished with count: " + localCount);
}
}
// Shared resource class with synchronization
class SharedResult {
private int totalWords = 0;
private final List<String> patternMatches = new ArrayList<>();
private final Lock lock = new ReentrantLock();
// synchronized method
public synchronized void addWordCount(int count) {
totalWords += count;
}
// lock-based protection
public void addPatternMatch(String match) {
lock.lock();
try {
patternMatches.add(match);
} finally {
lock.unlock();
}
}
public int getTotalWords() {
return totalWords;
}
public List<String> getPatternMatches() {
return patternMatches;
}
}
public class Practical2 {
public static void main(String[] args) {
String filePath = "large_text.txt"; // replace with your file path
List<String> allLines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
allLines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
int numThreads = 4; // divide work among 4 threads
int chunkSize = allLines.size() / numThreads;
SharedResult sharedResult = new SharedResult();
List<Thread> threads = new ArrayList<>();
// create threads
for (int i = 0; i < numThreads; i++) {
int start = i * chunkSize;
int end = (i == numThreads - 1) ? allLines.size() : (i + 1) * chunkSize;
List<String> subList = allLines.subList(start, end);
Thread t = new WordCountTask(subList, sharedResult, "Worker-" + (i + 1));
threads.add(t);
t.start(); // thread lifecycle: created → running
}
// wait for all threads to finish
for (Thread t : threads) {
try {
t.join(); // wait for termination
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// final result
System.out.println("\n=== Final Result ===");
System.out.println("Total word count: " + sharedResult.getTotalWords());
System.out.println("Pattern matches:");
for (String match : sharedResult.getPatternMatches()) {
System.out.println(match);
}
}
}