user1131338
user1131338

Reputation: 51

Java Parallel Programming Performance suggestion

I am building option valuation (binomial tree) using java . My binomial tree with normal sequential code with 1000 steps takes 0.1 second . But when i try to run it using parallel it takes about 65 seconds . Any suggestion and input will be appreciated..

Binomial Thread is my class which does the calultion
ThreadPoolExecutor threadExecutor = new ThreadPoolExecutor(10000, 10000, 500,
       TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),
       new ThreadPoolExecutor.CallerRunsPolicy());

for (int i = 0; i < 10000; i++) {
Runnable thread = new BinomialThread();
threadExecutor.execute(thread);
}

Upvotes: 2

Views: 94

Answers (2)

javaHunter
javaHunter

Reputation: 1097

For an N cpu processor. The optimal number of threads should is N+1.(Check Java Concurency in Practice Chapter 8). Once you create threads beyond this number(for compute intensive tasks), performance starts to decrease because of the context switches involved. Thus this explains the numbers you were getting.

To determine the number of cpus:

int N_CPUS = Runtime.getRuntime().availableProcessors();

You then should configure the executor to N+1 threads.

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

If the number of threads is greater than the number of processors and there is no waiting time (for example waiting from network data, reading from a file and so on) the performance degrades instead of increasing for the time needed to switch from one process to the other. You have 10.000 threads, that's why your program take more time in a multithreading environment than a single thread environment.

Upvotes: 2

Related Questions