Prachi Tripathi
Prachi Tripathi

Reputation: 179

Response time Increases as concurrency increases in Java

There is a standalone java program that I have written which involves one recursive function which is called atleast 60 thousand times. Now when I run this java program as a single thread it takes approx 2 seconds. However when I spawn 25 parallel threads the response time increases to 10 seconds where each thread starts at the same time and end also approximately at the same time after 10 - 15 seconds.

I ran the profiler tool (jvisualvm) to check if there is any lock on any thread that is being taken but all 25 show in running state at all times.

Hence my questions are: 1. In concurrency response time may increase (depends on cores on the system) , however if suppose the machine on which test runs is a 6 core machine then atleast 6 threads should finish in atleast 2 seconds or little more? Not all should complete processing at the same time ie. after 10 seconds? 2. How do i detect what is it that is causing problem in parallel processing?

Appreciate response on the same.

Thank you.

Upvotes: 1

Views: 1589

Answers (3)

Prachi Tripathi
Prachi Tripathi

Reputation: 179

My response time improved by warming up the threads. A lot of time was taken by thread creation which will improve when we run the code in an app server (like tomcat)

Another noticeable feature was after the first run, the second run was slower which makes me to believe the way classes are loaded by JVM at runtime (Apart from thread warmup)

Thank you all for your inputs

Upvotes: 0

MadConan
MadConan

Reputation: 3767

This is a pretty broad question. So I'm going to give a pretty broad answer.

  1. In concurrency response time may increase (depends on cores on the system) , however if suppose the machine on which test runs is a 6 core machine then atleast 6 threads should finish in atleast 2 seconds or little more?

Completely depends on the type of work being done. With N cores, the creation of N threads involves non-trivial overhead, as well as the parent process managing IO between cores. Multiple threads may help or hurt. Multithreading is usually a good bet if you have tasks that perform a lot of off-cpu IO, or when you have long running calculations on data that is not shared between threads.

  1. How do i detect what is it that is causing problem in parallel processing?

This can be tricky. Use a good debugger that pauses all threads at points. Look for many shared resources between threads, excessive/unnecessary synchronization, or long wait times. There are many possible reasons.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533880

In concurrency response time may increase (depends on cores on the system) , however if suppose the machine on which test runs is a 6 core machine then at least 6 threads should finish in at least 2 seconds or little more?

In theory, this is possible, but it depends entirely on what you are doing. If you are CPU bound and you have a problem which naturally parallelises, you can get 6x improvement with 6 code. If you have an network IO bound process, then 100 threads can be 100x faster with just 6 cores. However if you have a task where the overhead is too high, you can use orders of magnitude slower with multiple thread than just using one.

Not all should complete processing at the same time ie. after 10 seconds?

That is highly unlikely, but possible.

How do i detect what is it that is causing problem in parallel processing?

When you use multiple threads it is slower than one thread. I suggest you look at how you are break up the problem and making sure you don't have more over head than useful work.

Upvotes: 3

Related Questions