-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask3.java
More file actions
34 lines (28 loc) · 1.24 KB
/
Copy pathTask3.java
File metadata and controls
34 lines (28 loc) · 1.24 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
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
class BankStatementBatchProcessor {
// FIX: AtomicInteger makes increments thread-safe; int++ is a non-atomic read-modify-write operation.
private final AtomicInteger processedCount = new AtomicInteger(0);
public void process(List<StatementRecord> records) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(10);
for (StatementRecord record : records) {
executor.submit(() -> {
processRecord(record);
// FIX: Use atomic increment so concurrent worker threads cannot overwrite each other's updates.
processedCount.incrementAndGet();
});
}
executor.shutdown();
executor.awaitTermination(5, TimeUnit.MINUTES);
}
public int getProcessedCount() {
// FIX: Read the current atomic value instead of returning a shared mutable int directly.
return processedCount.get();
}
private void processRecord(StatementRecord record) {
// Existing processing logic remains unchanged.
}
}