Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to create multiple thread in a java program for analyzing comments?

User_40RXJSep 16 2022

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);
}

}

Comments

EdStevens

This really has nothing to do with SQL Developer (the subject of this forum). It is a much better fit in SQL & PL/SQL.
And just as a side comment, storing 'age' as data is a flawed design. The 'age' of everyone and every thing is increasing by the day, if not by the second. What is your plan to keep 'age' current? Better to store 'date of birth' and the calculate 'age' when needed at run-time.

User_H3J7U

The 'age' of everyone and every thing is increasing by the day, if not by the second.
Sometimes the age stops increasing.
изображение.png

EdStevens

Pour me one!
Of course, that's not actually the age, but the amount of time it was 'aged' in the barrel before bottling.

1 - 3

Post Details

Added on Sep 16 2022
1 comment
166 views