1

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

5
  • 1
    Read the file content in the main thread and fill your array. After that, create each thread with responsibility to find the max from the given distinct section of the array. Once each thread completes finding the max, the main thread can go through their output to find the overall max as pointed out by Jiri. Commented Oct 26, 2015 at 21:14
  • You will need your main thread to wait for the other threads to complete before it is calculating the overall max. Commented Oct 26, 2015 at 21:15
  • @IzCe Thanks. So I have to get it to search a certain part of the array? hmm... I guess with a for loop? Maybe assign thread 1 to look through index 0 to 999, thread 2 - 1000 to 1999. etc? Not sure how to go about that, I'll try though. Would you say I should create threads like I did above? ex: 'Thread thread2 = new Thread() - Or is there a better way to create 10 threads without doing them individually? Commented Oct 26, 2015 at 21:53
  • the start and end indices will look like what you mentioned in your case. However, I would do it parametric and calculate them based on the number of data entries (randomNumbersCount) and the number of threads (threadCount). Then I would refactor PDF class to use it like (1) PDF [] threads = new PDF[threadCount]; (2) in a loop: PDF[i] = new PDF(randomNumbers, startIndex, endIndex, threadResult); (3) in another loop: PDF[i].start(); (4) yet in another loop: PDF[i].join(); to enforce that the main thread will wait for completing the others. (5) main thread, loops through threadResult objects. Commented Oct 27, 2015 at 8:14
  • Thread result can be defined simply as 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. Commented Oct 27, 2015 at 8:18

1 Answer 1

2

Both threads are reading the whole file, so they both do the same, full computation. You'll probably want either each thread to only read part of the file, or one thread reading the file and delegating the computation to the other threads and collecting their results (i.e. computing the maximum of the max values returned by all threads).

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm.. I would like one thread to read the file but delegate the other threads to handle the computation and collect the results. So to do this, I would delegate threads to read certain parts of the array (txt file)?
Either give them reference to the (same) array and (different) start and stop indices, or read the input into a List and use subList().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.