0

So i have a MaxThread class and the run() method stores the maximum value from the vector passed, into maxValue class attribute. I want to split my initial array of 10 integers into 4 subarrays and each subarray to have a different thread. After i find the 4 maximum values from each one of the 4 subarrays, i want to create a new MaxThread and to display the maximum value from it. this code works but i cant think of a better way to do it as I'm sure what i did is silly( I'm new to this).

public class Main{

    public static void main(String[] args) {
        int step = 3;
        Integer[] vector = new Integer[10];
        readFromFile(vector);
        int i = 0;
        ArrayList<MaxThread> threads = new ArrayList<>();
        MaxThread t1 = null;
        MaxThread t2 = null;
        MaxThread t3 = null;
        MaxThread t4 = null;

            t1 = new MaxThread(vector, 0, 3);
            threads.add(t1);
            t1.start();

            t2 = new MaxThread(vector, 3, 6);
            threads.add(t2);
            t2.start();

            t3 = new MaxThread(vector, 6, 9);
            threads.add(t3);
            t3.start();

            t4 = new MaxThread(vector, 9, 10);
            threads.add(t4);
            t4.start();


        for (MaxThread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Integer[] last = new Integer[4];
        last[0]=t1.getMaxVal();
        last[1]=t2.getMaxVal();
        last[2]=t3.getMaxVal();
        last[3]=t4.getMaxVal();

        MaxThread lastThread = new MaxThread(last, 0, last.length);
        lastThread.start();
        try {
            lastThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Valoarea maxima:" +lastThread.getMaxVal());
    }
2
  • Looks good to me. Besides the obvious flaw that you correctly decided to put your numbers into an array, but then go to have 4 distinct variables for your threads. To then be forced to handle them manually. Commented Jun 9, 2019 at 16:04
  • @GhostCat I'm not pleased with they way i divided the initial array. Is there a better way to do that? Also I knwo I need to find a better solution for using the 4 max values into a new thread. Commented Jun 9, 2019 at 16:17

1 Answer 1

1

This is a good case for parallel streams. In general multiple threads are only worth it if you have at least a couple of tens of thousands of elements. In these cases it also pays to have a primitive array.

public static void main(String[] args) {
    int[] vector = // ...
    int max = Arrays.stream(vector).parallel().max().getAsInt();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Parallel stream is the way I would do it. Ad hoc frameworks like the OP's should be avoided. In a more general case, look into Java's Fork-Join framework.
I did a bit of benchmarking the other day (stackoverflow.com/questions/56488685/…). 2 million entries were not enough to get the parallel stream solution to be faster than the sequential stream :-o
That seems broken, frankly.
@GhostCat I wrote a JMH microbenchmark that suggests they are about equal with 10'000 elements and parallel streams are faster with 100'000 elements github.com/marschall/jmh-experiments/blob/parallel-stream/src/…

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.