java - Summing an array using multithreading -


i have array size of n filled numbers 1..n.

i need sum array using m threads each time taking 2 elements, sum them , inserting sum array.

here tried do.

the synchronized part first

public class multithreadedsum {  private arraybuffer arraybufferinst; private int sum; private boolean flag;  public multithreadedsum(arraybuffer arraybufferinst) {     this.arraybufferinst = arraybufferinst;     sum = 0;     flag = false; }  public synchronized void sum2elements() {      while(flag)     {         try {wait();}         catch (interruptedexception e){}     }      flag = true;      if (arraybufferinst.retunrsize() < 2)     {         return;     }      system.out.println("removing 2 elements.");      sum = arraybufferinst.sum2elements();      notifyall(); }  public synchronized void insertelement() {      while(!flag)     {         try {wait();}         catch (interruptedexception e){}     }      flag = false;      system.out.println("inserting sum.");      arraybufferinst.insertelement(sum);      notifyall(); }  public int retunrsize() {     return arraybufferinst.retunrsize(); }  } 

i've splitted m threads 2 groups, half of them summarization , half adding using wait , notify.

public class sum2elementsthread implements runnable{      private multithreadedsum multithreadedsuminst;          public sum2elementsthread( multithreadedsum multithreadedsuminst)     {         this.multithreadedsuminst = multithreadedsuminst;     }      @override     public void run() {          while(multithreadedsuminst.retunrsize() > 1)         {             multithreadedsuminst.sum2elements();         }      }  }  public class insertthread implements runnable{      private multithreadedsum multithreadedsuminst;          public insertthread( multithreadedsum multithreadedsuminst)     {         this.multithreadedsuminst = multithreadedsuminst;     }      @override     public void run() {          while(multithreadedsuminst.retunrsize() > 1)         {             multithreadedsuminst.insertelement();         }      }  } 

here part of main:

arraybufferinst = new arraybuffer(n);  multithreadedsuminst = new multithreadedsum(arraybufferinst);  executorservice threads = executors.newcachedthreadpool();  (i = 0; < m/2; i++) {     threads.execute( new sum2elementsthread(multithreadedsuminst) ); }  (; < m; i++) {     threads.execute( new insertthread(multithreadedsuminst) ); }  while (multithreadedsuminst.retunrsize() > 1){}  threads.shutdown(); 

and buffer

public class arraybuffer {      private arraylist<integer> arraybufferinst;      public arraybuffer(int sizeofbuffer)     {         int i;          arraybufferinst = new arraylist<>(sizeofbuffer);          (i = 0; < sizeofbuffer; i++)         {             arraybufferinst.add(i, i+1);         }     }      public int sum2elements()     {         if (arraybufferinst.size() < 2)         {             return -1;         }          return arraybufferinst.remove(0) + arraybufferinst.remove(1);     }      public void insertelement(int elem)     {         arraybufferinst.add(elem);     }      public int retunrsize()     {         return arraybufferinst.size();     } } 

i'm getting lot of java.lang.indexoutofboundsexception , rangecheck errors, may way implemented sum2elements i'm not sure.

any guys?

thanks.

when called on arraylist {a, b, c}

    if (arraybufferinst.size() < 2)     {         return -1;     }      return arraybufferinst.remove(0) + arraybufferinst.remove(1); 

then arraybufferinst.remove(0) remove , return a, changing arraybufferinst {b, c}. calling arraybufferinst.remove(1) remove c, not want. change 1 0.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -