Solved

Synchronized/Threads - Checking for Bottlenecks

Posted on 2004-09-07
15
248 Views
Last Modified: 2010-03-31
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
Comment
Question by:amacfarl
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11996080
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11996090
...and you can also record the time *before* it calls the synched method
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 20 total points
ID: 11996098
>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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 11996119
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
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 25 total points
ID: 11996292
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
 
LVL 2

Author Comment

by:amacfarl
ID: 11999911
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 60 total points
ID: 11999979
>>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
 
LVL 2

Author Comment

by:amacfarl
ID: 12001248
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
 
LVL 92

Expert Comment

by:objects
ID: 12001274
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12001397
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
 
LVL 92

Expert Comment

by:objects
ID: 12001437
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12001479
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12001503
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12001660
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12001675
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 75
difference of if loops 23 62
Html Table Looping (part 2) 5 27
collection output issue 9 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question