The Government
The Government

Reputation: 21

How to ensure parallel processing in Java

I have a static Array, arr, whose elements are getting squarred and stored back again using the 'squarring' method. I want two threads to simultaneously modify the array. Each thread works on half of the array.

public class SimplerMultiExample {

    private static int[] arr = new int[10];
    public static void squarring(int start, int end)
    {
        for(int i=start; i<end; i++)
        {
            arr[i]*=arr[i];
            System.out.println("here "+Thread.currentThread().getName());
        }
    }


    private static Runnable doubleRun = new Runnable() {
        @Override
        public void run() {
        if(Integer.parseInt(Thread.currentThread().getName())==1)
            squarring(0,arr.length/2);  //Thread named "1" is operaing on
                                        //the 1st half of the array.
        else
            squarring(arr.length/2,arr.length);
        }
     };

    public static void main(String[] args){

         Thread doubleOne = new Thread(doubleRun);
         doubleOne.setName("1");

         Thread doubleTwo = new Thread(doubleRun);
         doubleTwo.setName("2");

         doubleOne.start();
         doubleTwo.start();
    }
}

The sysout in the 'squarring' method tells me that the threads are going into the method serially, that is, one of threads finishes before the other one accesses it. As a result, one of the threads finishes early whereas the other ones takes considerably longer to complete. I have tested the code with 1 million elements. Please advice on what I can do to ensure that the threads operate in parallel. P.S - I am using a dual core system.

Upvotes: 2

Views: 335

Answers (2)

beatngu13
beatngu13

Reputation: 9333

You don't have to program this from scratch:

Arrays.parallelSetAll(arr, x -> x * x);

parallelSetAll creates a parallel stream which does all the work for you:

Set all elements of the specified array, in parallel, using the provided generator function to compute each element.

If you'd like to know how to control the number of threads used for parallel processing, checkout this question.

Upvotes: 1

Austin
Austin

Reputation: 8565

I recommend you add the following code to the end of your main method to ensure they each finish their work:

     try {
        doubleOne.join(); //Waits for this thread to die. 

        doubleTwo.join(); //Waits for this thread to die. 
     } catch (InterruptedException e) {
        e.printStackTrace();
    }

You are creating two threads that could be executed in parallel provided that the scheduler chooses to interleave them. However, the scheduler is not guaranteed to interleave the execution. Your code works in parallel on my system (...sometimes.. but other times, the threads execute in series).

Since you are not waiting for the threads to complete (using the join method), it is less likely that you would observe the interleaving (since you only observe part of the program's execution).

Upvotes: 0

Related Questions