java - How to select optimum number of threads -


actually wrote java program calculating particular number on fibonacci series.

the problem right using number of cores number of threads required. have observed input size increases better performance increasing number of threads.

is there existing formula/theory on how divide problem multiple threads?

below source code:

import java.util.concurrent.executionexception; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future;  public class fibonacci {     private static long[] value;      public static void main(string args[]) throws interruptedexception {         int n;         try {             n = integer.parseint(args[0]);         } catch (exception e) {             throw new runtimeexception(                     "please enter in form java n number ");         }         value = new long[n + 1];           long start = system.nanotime();         int nthreads = runtime.getruntime().availableprocessors();         executorservice executorservice = executors                 .newfixedthreadpool(nthreads);         int result;         try {             result = fibonaccisum(n, executorservice);         } catch (executionexception e) {             throw new runtimeexception("thread interuppted ");         }         system.out.print(" multithreading = " + result);         long end = system.nanotime();         system.out.println("\t time = " + (end - start) + "ns");     }       private static class fibonaccithread implements runnable {         int index;         int result;         executorservice executorservice;          public fibonaccithread(int index) {             this.index = index;         }          public void run() {             try {                 this.result = fibonaccisum(index, executorservice);             } catch (exception e) {                 throw new runtimeexception("thread interupted");             }         }     }      private static int fibonaccisum(int index, executorservice executorservice)             throws interruptedexception, executionexception {         if (index <= 2) {             return 1;         } else {              fibonaccithread fibonaccithread1 = new fibonaccithread(index - 2);             fibonaccithread1.executorservice = executorservice;             future future = executorservice.submit(fibonaccithread1);             object object = future.get();             int resultpart2 = fibonaccisum(index - 1, executorservice);             int result = fibonaccithread1.result + resultpart2;             // executorservice.shutdown();             return result;         }     }  } 

in case haven't realized this, fibonacci numbers not candidate parallelization. (increasingly) better performance:

  • by using single threaded recursive algorithm,
  • by using single threaded recursive algorithm memoization, and
  • by solving recurrence relation , implementing resulting formula.

the basic problem naive fibonacci calculation requires exponential number of computation steps. can't make impact on (in terms of performance) multi-threading because have bounded number of processors.

even if did have infinite number of processors, thread setup / task creation overheads exceed time doing step in calculation.

finally, there particular problem doing fibonacci using executor service , bounded thread pool. if thread pool small (or n large) computation liable stuck. instance, way you've coded needs ~n threads in pool calculate fibonacci(n), though of them blocked of time. (the best way understand "hand execute" application, paying attention how many threads in use @ each point in time ... , doing.)


so simple answer question (for particular application) need @ least n threads in pool complete computation of fibonacci(n). (this not general answer. mandated details of algorithm you've implemented.)


there lots of other questions / answers on calculating fibonacci numbers using threads. suggest read them well.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -