Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
How to create multiple thread in a java program for analyzing comments?

Hi Team
I have a program that analyse comments per single line, how do i then multiple thread to it to allow more or seperate thread? Currently programming running sequentially only.
//Current code
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CommentAnalyzer {
private File file;
public CommentAnalyzer(File file) {
this.file = file;
}
public Map<String, Integer> analyze() {
Map<String, Integer> resultsMap = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (line.length() < 15) {
incOccurrence(resultsMap, "SHORTER_THAN_15");
} else if (line.contains("Mover")) {
incOccurrence(resultsMap, "MOVER_MENTIONS");
} else if (line.contains("Shaker")) {
incOccurrence(resultsMap, "SHAKER_MENTIONS");
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + file.getAbsolutePath());
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO Error processing file: " + file.getAbsolutePath());
e.printStackTrace();
}
return resultsMap;
}
/**
* This method increments a counter by 1 for a match type on the countMap. Uninitialized keys will be set to 1
* @param countMap the map that keeps track of counts
* @param key the key for the value to increment
*/
private void incOccurrence(Map<String, Integer> countMap, String key) {
countMap.putIfAbsent(key, 0);
countMap.put(key, countMap.get(key) + 1);
}
}
Answers
-
i think the answer from this stackoverflow would help you
https://stackoverflow.com/questions/18971951/multithreading-to-read-a-file-in-java