I have a ArrayList [big one read from a file] And I want to read its contents with multithreading and process each string calling a method repeatedly and printing it to a file .I have given a working structure of what my the code looks like.. How ever I a not able to code for what I want without getting tangled in exceptions related to synchronizations of threads ... I am new to the concept of threading .. and want a efficient way to to this ..I have looked at other solutions related to threading and arraylists but it hasn't worked out for me .. any suggestions as to how to go about this is appreciated
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class threadingWithMathod {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
ArrayList<String> samples=readurls("path/to/sample.csv");
PrintStream filewriter = new PrintStream(new File("path/to/result.csv"), "UTF-8");
for (int i = 0; i < samples.size(); i++) {
String string1 = samples.get(i);
String string2 = samples.get(i+1);
///Need Info As to how process with Threading without clashing
/// sampleProcessString need to be called repeatedly
//sampleProcessString(filewriter,string) by 2-3 threads
}
}
public static void sampleProcessString(PrintStream filewriter,String string) {
filewriter.println(processedString(string));
}
private static Object processedString(String string) {
//Intended to generate a new line by using a Sql query
//This method will be using a connection to a mysql data base based on sample
return string+"++> done something";
}
public static ArrayList<String> readurls(String filename) {
ArrayList<String> aslink=new ArrayList<String>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader( filename));
String line = reader.readLine();
while (line != null) {
aslink.add(line);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return aslink;
}
}