Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Synchronized/Threads - Checking for Bottlenecks

Posted on 2004-09-07
15
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60 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
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.

 
LVL 92

Assisted Solution

by:objects
objects earned 60 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 75 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 180 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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