Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Synchronized/Threads - Checking for Bottlenecks

Hi Everyone,

We have an application that uses multiple threads to perform a calculation. When any method has found a possible "Solution" it writes to a database.

We want to track the performance of the application and the number of calculations that it has performed, therefore, we have implemented a single class which has Synchronized methods to count the number of calculations performed.

If a Thread is currently using the "Synchronized Counter Method" and another method tries to access it - it waits until the Thread has finished. What I would like to find out is how many "waits" are occuring. I.e. is the counting aspect slowing down the application by causing a bottle neck.

With approximately 680 calculations per minute. This is a concern.

Is there anyway determine if these "waits" are occuring? Due to the nature of the program it is not possible to switch off the counting and switch it back on again and compare the executing time.

Kind regards
Angus
0
amacfarl
Asked:
amacfarl
  • 6
  • 5
  • 2
  • +2
4 Solutions
 
CEHJCommented:
You don't need anything special. Since you're the one creating the threads, simply keep a count and record the time when each thread reports it's finished
0
 
CEHJCommented:
...and you can also record the time *before* it calls the synched method
0
 
Giant2Commented:
>680 calculations per minute
so about 11 for each second.
I believe this is a little bottleneck.
You can do:
calculation
store this calculation in a local variable
calculation
store...
...
calc..
store
when finish the calculation, STORE ALL the calculation done

This method is memory consuming, but you are sure that the synchronization mechanism doesn't take any time during the calculation.


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
have each thread keep track of how many calculations it has performed, that way they don't need to access any synchronised method.
and if you need to get the total then sum up all the threads.
It'll take a little longer to get the count, but that aint critical and you remove any performance impact on the real task at hand.
0
 
cjjcliffordCommented:
two possibilities.

1. Reduce the synchronization - if there are several tasks being performed in the "counting" method, but only some of them are core (i.e. that have to be synchronized), move the synchronization from the method into the body:

public synchronized void count() {
    // unsync code
    // sync code
    // unsync code
}

would become:

public void count() {
   // unsync code
   synchronized( this ) {
       // sync code
   }
   // unsync code.
}

2. Change from a single class to a Factory class that creates objects of count monitors. each calculation class would use its own counter class, and there would be one single class doing reporting - still some synchronization required for this, as periodically the reporting class would require to fetch data correctly from each of the counter classes - however, this would be much less frequent than the frequency of count changes, etc...
0
 
amacfarlAuthor Commented:
Wow... was not expecting such a response... thanks.

You have provided me with some very good ideas on how to remove the bottle neck.  much apprecaited.

The application is very complex and changing the handling of threads will involve significant development work.  Therefore what I would like to establish before making any changes is that indeed this is creating a bottleneck (& a significant one).

I have tried using the suggestions, e.g. time etc. but was unable to distinguish if there was indeed a bottleneck as there are 40+ Threads.  What would be extremely useful is to be able to catch the 'wait' Thread when a thread tries to access the count method.  And therefore after processing is finished, establish the number of waits (and maybe how long).

Once again - cannot thank you guys enough for replying to my query.
0
 
CEHJCommented:
>>What would be extremely useful is to be able to catch the 'wait' Thread when a thread tries to access the count method.

That's what i suggested:

...

log.debug("Thread " + getName() + " calling foo");
foo();
....

public synchronized void foo() {
    log.debug("Thread " + Thread.currentThread().getName() + " entering foo");
}

The wait time is more or less the difference between the two debug timestamps
0
 
amacfarlAuthor Commented:
Sorry CEHJ I miss understood you.

Thanks for the code - it has worked.. Indeed I have a bottle neck.  Damn.  Back to the drawing board... this is going to be a long night.

ALL - Once again thanks for the tips.. together you have given me lost of food for thought.
0
 
objectsCommented:
> Indeed I have a bottle neck.

Yes, any synchronisation will introduce a bottle neck.
Even if they never have to wait it still adds overhead to the method call.
0
 
CEHJCommented:
This is potentially the only synchronized method you need your 'calculator' to call:

public void synchronized countCalc(YourRunnable calculator) {
    calcsDone += calculator.getCalcsDone();
}

The time spent in that method will be negligible
0
 
objectsCommented:
Time spent in the method is not the only issue, just calling a synchronised method adds overhead.

The suggestion I posted above would remove any need for synchronisation (as well as the need to call any method).
0
 
CEHJCommented:
>>just calling a synchronised method adds overhead

The overhead of calling that method is negligible

>>The suggestion I posted above would remove any need for synchronisation (as well as the need to call any method).

...and would be more difficult to implement, quite apart from possibly causing much worse overheads by having to retain references to all runnables until the last one finishes or the overhead of creating a polling thread
0
 
objectsCommented:
amacfarl,

try it out and see for yourself :)
Do some benchmarking with you current code, and then comment out the call to the synchronised method, run it again and see the difference.
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
Thanks amacfarl, let me know if you want to discuss my suggestion further it should give you a decent improvement in performance.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now