0

This is my first time trying to work with multithreading so help would be appreciated!

Following problem:

The user enters values for the array, as well as desired quantity of threads to the console. The array should be summed using those threads and each thread finds the sum of its own chunk in array. The summed result should be printed for each thread and also overrall sum.

public class Thread1  extends Thread{

    @Override
    public void run() {
        System.out.println("Enter the required size of the array :: ");
        Scanner s = new Scanner(System.in);
        int size = s.nextInt();
        int[] myArray = new int [size];
        System.out.println("Enter the required quantity of the threads :: ");
        int k = s.nextInt();
        int sum = 0;
        System.out.println("Enter the elements of the array one by one ");
        
        for(int i=0; i<size; i++) {
            myArray[i] = s.nextInt();

            for(int j = 0; j< k; j++){
                sum = sum + myArray[i];

            }
            System.out.println("Sum for thread: " + this.getName() + " = " + sum);
        }
        System.out.println("Elements of the array are: "+ Arrays.toString(myArray));
        System.out.println("Sum of the elements of the array ::"+sum);
    }

    public static void main(String[] args) {
        Thread1 t = new Thread1();
        t.start();



    }
}

3
  • Hi Yakov, welcome to SO. Please, you have to tell us one specific question and provide a sample with your effort. I suggest you to read this thread, try it and then, with some working example, we will try to help you. dzone.com/articles/… Commented Nov 11, 2020 at 13:15
  • Ehh I mean I know how to find the sum of array. What I don't understand is to how assign chunks of array to each thread(sorry If I'm not making myself clear, english is not my native language). I added some changes but I think it's not right Commented Nov 11, 2020 at 14:43
  • you can find out the solution on URL below. codereview.stackexchange.com/questions/123305/… Thank you :) Commented Dec 17, 2021 at 3:09

1 Answer 1

1

As this dates back to a year ago I would mention how I approached this. Firstly, as using ExecutorService is more encouraged I used this to do multithreading. It's also worth mentioning that I haven't used synchronize or locks as I won't share any value and the sum will be computed at the end instead of each tread adding to one common sum.

public void arraySum(int[] arr){
        start = System.currentTimeMillis();
        ExecutorService exec = Executors.newSingleThreadExecutor();
        sum = 0L;
        int noThreads = 100;
        int length = arr.length/noThreads;
        
        List<Long> res = new ArrayList<>();
        for(int i=0;i<noThreads;i++) {//each thread
            int low = length*i;
            int high = length+low;
            
            res.add(exec.submit(callable(arr, low, high)).get());
        }
                
        exec.shutdown();
        while(!exec.isTerminated());
        if(exec.isTerminated()) {           
            for(long l:res) {               
                sum+= l;
            }
        }
        finish = System.currentTimeMillis();
        System.out.println(noThreads+" threads: Sum: "+sum+" Complettion in miliseconds: "+(finish -start));
        
    }

    Callable<Long> callable(int[] arr,int low, int high){
        return ()->{
            Long res = 0L;
            for(int i=low;i<high;i++) {
                res += arr[i];
            }           
            return res;
        };      
    }

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

2 Comments

if you want to use this make sure you fix the partitioning part as it is using integer division so some elements might be left out in the upper part.
be careful about how you invoke that method arraySum with the executor service inside it. It is a heavy object that should be declared outside of the method.

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.