java - Multiple Threads spawning multiple threads and how to wait for child Threads -
i have 2 arrays abc[100] , def[1000] , have find array xyz[100] , xyz[i] = mindistance(abc[i],def) i.e every element in abc have find corresponding nearest element in def , set in xyz.
for using threads @ 2 level . @ first level creating threads every 10 points in abc , @ second level each creating child threads every 100 points in def. below implementation .
my questions
how can wait child threads of
abc(i.edefthreads) . have gone through java join method not able figure out on how use .can use cyclic barrier in case.
the actual data in magnitude 1000s
abc, 10000def, haven't used threads before ,so there issues can happen implementation . have seen use ofthreadpoolexecutorinstead offixedthreadsin examples couldnt figure out howthreadpoolexecutorhave.
1. distancecalculation
public class mindistancecalculation { public static list<double[]> xyz = new vector<double[]>(); public void method1(){ double[][] abc = new double[100][7]; double[][] def = new double[1000][7]; executorservice executorservice = executors.newfixedthreadpool(10); for(int = 0 ; < abc.length ; = i*10){ executorservice.execute(new mainthread(abc,i , i*10 , def)); } } } 2 . main thread / abc threads
public class mainthread implements runnable{ double[][] abc = null; double[][] def = null; int startpos = 0; int endpos = 0; public mainthread(double[][] abc , int startpos , int endpos, double[][] def){ this.abc = abc; this.def = def; } @override public void run() { for(int = startpos ; < endpos ; i++){ executorservice executorservice = executors.newfixedthreadpool(10); list<future<double[]>> mindistancepoints = new arraylist<future<double[]>>(); for(int j = 0 ; j < def.length ; j = j*100 ){ future<double[]> mindistancepoint = null; mindistancepoint = executorservice.submit(new childthread(abc[i], def, j, j*100)); mindistancepoints.add(mindistancepoint); } // how can wait threads , calculate mindistance , //add actual array findmindistanceofall(abc[i],mindistancepoints); executorservice.shutdown(); } } public void findmindistanceofall(double[] mainpoint, list<future<double[]>> distancepoints){ // here find min among given points , add actual array. mindistancecalculation.xyz.add(null); } }
child thread / def threads
public class childthread implements callable<double[]> { double[] abc = null; double[][] def = null; int from; int to; public childthread(double[] abc, double[][] def, int from, int to) { this.def = def; this.abc = abc; this.from = from; this.to = to; } @override public double[] call() throws exception { double mindistance = double.max_value; double currentdistance = 0; double[] mincandidate = null; (int = from; < to; i++) { currentdistance = distance(abc,def[i]); if (currentdistance < mindistance) { mindistance = currentdistance; mincandidate = def[i]; } } return mincandidate; } public double distance(double[] point1 , double[] point2) { // calculates , returns euclidean distance return 0; } }
determine parallel task should do. best parallelization when there minimal interaction. calculating 1 element of xyz array best candidate. splitting def in 10 chunks bad because chunks not independent. combining 10 elements of abc in 1 thread may have sense when want increase size of task , reduce task's interaction, not evident optimization , should done later.
decide how run these tasks. wrapping each task in separate runnable , submitting thread pool universal way here can avoid this. each tack identified index abc array (and xyz array). can keep current index in atomicinteger , use getandincrement obtain next index.
since task cpu-bound, start n threads n=number of available processors.
count number of finished tasks countdownlatch.
add initialization , min distance calculation here:
public class mindistancecalculation implements runnable { atomicinteger idx=new atomicinteger(); int inpsize=100; double[] abc = new double[inpsize]; double[] def = ... double[] xyz = new double[inpsize]; countdownlatch counter=new countdownlatch(inpsize); public void run() { (;;) { int nextindex=idx.getandincrement(); if (nextindex>=inpsize) return; xyz[nextindex]=mindistance(abc[nextindex], def); counter.countdown(); } void start() { (int k=0; k<runtime.getruntime.availableprocessors()) { new thread(this).start(); } counter.await(); } public static void main(string[] a) { new mindistancecalculation().start(); } }
Comments
Post a Comment