Partitioning and analyzing a java array with multithreaded processing -


i have initialize float[12000] via loop 12000 times. scan array values exceeding threshold value. if value exceeds threshold, manipulate instance variable of object.

example:

random random = new random(); float[] x = new float[12000];  (int = 0; < x.length; i++) {   x[i] = random.nextfloat(); }  (int = 0; < x.length; i++) {   if (x[i] >= 0.75) {   \\ interesting   } } 

basically, have change values of array , 12000 times on new array each time of length 12000. "something interesting" code merely looking index in data structure , calling setter. system time calculations, should take me 13 hours. have 8 processors on machine.

how can take advantage of java's multi-threading capabilities? looking thread solutions partition initializing , scanning of arrays. source code using threads appreciated.

you can divide among 8 different threads doing this

public class worker implements runnable {     final private int minindex; // first index, inclusive     final private int maxindex; // last index, exclusive     final private float[] data;      public worker(int minindex, int maxindex, float[] data) {         this.minindex = minindex;         this.maxindex = maxindex;         this.data = data;     }      public void run() {         for(int = minindex; < maxindex; i++) {             if(data[i] >= 0.75) {                 // interesting             }         }     } }   // *** main thread *** float[] data = new float[12000]; int increment = data.length / 8; for(int = 0; < 8; i++) {     new thread(new worker(i * increment, (i + 1) * increment, data)).start(); } 

this divides array among 8 different threads. or, option this:

public class worker implements runnable {     final private blockingqueue<integer> queue;     final private float[] data;      public worker(blockingqueue<integer> queue) {         this.queue = queue;         this.data = data;     }      public void run() {         while(true) {             int = queue.take();             float f = data[i];             // interesting f         }     } }   // *** main thread *** blockingqueue<integer> queue = new linkedblockingqueue<>(); float[] data = new float[12000]; for(int = 0; < 8; i++) {     new thread(new worker(queue, data)).start(); } for(int = 0; < data.length; i++) {     if (data[i] >= 0.75) {         queue.offer(i);     } } 

this uses 1 thread iterate through array , find interesting numbers, , uses 8 worker threads interesting interesting numbers. i'd tend prefer approach, it's possible first approach 1 worker thread wind having process thousand interesting numbers while worker thread needs process few interesting numbers; approach ensures each thread needs process approximately same quantity of interesting numbers.

i'm omitting lot of stuff, how use executors , how shut down worker threads etc - here's tutorial on that.

edit take code , run 12000 times on 8 threads, following:

public class worker implements runnable {     private final int numberofiterations;     private final float[] x = new float[12000];      public worker(int numberofiterations) {         this.numberofiterations = numberofiterations;     }      public void run() {         for(int = 0; < numberofiterations; i++) {             random random = new random();              (int = 0; < x.length; i++) {                 x[i] = random.nextfloat();             }              (int = 0; < x.length; i++) {                 if (x[i] >= 0.75) {                     \\ interesting                 }             }         }     } }   // *** main thread *** thread[] threads = new thread[8]; for(int = 0; < 8; i++) {     threads[i] = new thread(new worker(12000/8));     threads[i].start(); } for(int = 0; < 8; i++) {     threads[i].join(); } 

each of 8 threads run 1500 iterations of "initialize float array, iterate through float array" code. join method wait threads finish. code in // interesting thread-safe - said you're calling setter, multiple threads won't calling same setter, or else setter synchronized, or else you're using atomicinteger in setter. post setter code if have doubts it.


Comments