java - How to access arrays from multi-thread? -


simply speaking, problem follows:

i have class named test , has 3 double arrays,named array1, array2,array3 respectively(assuming have same length).it has major function named estep, , need implement function multi-threading improve speed.

in estep, need modify array1 , array2, , array3 read.i define indicator variable named which_array indicator array modify.i passed 3 arrays estep , modify array1 , array2.

my code follows , test , it works fine modification of array1 , array2 in estep can seen in test class. still have doubt if absolutely right. i'm wondering if should add volatile 3 array1 in test class or should need synchronization mechanism?.

any advice appreciated!

public class test{      double[] array1;     double[] array2;     double[] array3;     //for simplifization, omitted code allocation , initialization of the arrays.     public void estepintest()     {         final countdownlatch countdown = new countdownlatch(2);      estep modifyarray1 = new estep(array1, array2, array3, 1,countdown);     thread t1 = new thread( firsthalf);     t1.start();      estep modifyarray2 = new estep(array1, array2, array3, 2,countdown);             thread t2 = new thread(secondhalf);     t2.start();      try{     countdown.await();     }catch (interruptedexception e) {         e.printstacktrace();     }          //do next things     }  }  class estep implements runnable{      double[] array1;     double[] array2;     double[] array3;     int which_array;      estep(double[] array1, double[] array2, double[] array3, int which_array, countdownlatch cd)     {         this.array1 = array1;         this.array2 = array2;         this.array3 = array3;         this.which_array = which_array;         this.cd = cd;     }      public void run(){          if( this.which_array == 1){            for( int = 0; < array1.length; ++i )            {                   array1[i] = array1[i] + array3[i];            }           }else{            for( int = 0; < array2.length; ++i )            {                   array2[i] = array2[i] + array3[i];            }         }         cd.countdown();    } 

it right. countdownlatch.await() guarantees done before returns happens-before done after has returned.

your code cleaner , shorter if passed array modify estep constructor, instead of passing 2 arrays, , magic number indicate 1 modify.

edit:

regarding access array3, it's safe well, because, explained in the documentation of concurrent package, call start on thread happens-before action in started thread. 2 threads see every modification made array3 has been made before starting them.

also, note making array volatile makes array reference volatile. doesn't make elements volatile.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -