I'm pretty new to Java and programming in general, so I'm having some difficulty here.
I'm currently trying to take a program I worked on a few weeks back and apply threading functionality to it. I would like get my Java program to read a text file entitled 'dataset529.txt' (which has 100k numbers in it) and do so with 10 parallel threads. I want it to read the file and tell me the largest number within it, but do so with 10 threads. I originally tried using a loop for the threads, but now I'm defining each thread individually like thread1, thread2 etc.
Right now I'm experimenting on how to do this. I'm not sure if I'm on the right track.
This is what I have so far
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ProcessDataFileParallel extends Thread implements Runnable{
public void ReadText() throws IOException{
String emptyString;
FileReader fr = new FileReader("dataset529.txt");
BufferedReader br = new BufferedReader(fr);
emptyString = br.readLine();
int max = Integer.MIN_VALUE;
int i = 0;
int[] randomNumbers = new int [100000];
while ((emptyString = br.readLine()) != null) {
randomNumbers[i++] = Integer.parseInt(emptyString);
}
for (i = 0; i < randomNumbers.length; i++)
if (randomNumbers[i] > max)
max = randomNumbers[i];
System.out.println("The Largest Number from 'CreateDataFile's' array is : "+ max);
}
public static void main (String[] args) throws IOException {
Thread thread1 = new Thread(){
public void run(){
ProcessDataFileParallel PDF = new ProcessDataFileParallel();
try{
PDF.ReadText();
}catch (IOException e){
e.printStackTrace();
}
}
};
Thread thread2 = new Thread(){
public void run(){
ProcessDataFileParallel PDF = new ProcessDataFileParallel();
try{
PDF.ReadText();
}catch (IOException e){
e.printStackTrace();
}
}
};
thread1.start();
thread2.start();
}
}
So far I just have two threads. I would like both threads to read the text file and give me what it thinks is the largest number within that text file. I think when I have multiple threads working simultaneously, it could give me different results.
Any help would be appreciated. Thanks
class ThreadResult { private int max; public void setMax(int max) { this.max = max; } }and then create a ThreadResult array and pass the respective object to the PDF in step 2 above.