Solved

Synchronized/Threads - Checking for Bottlenecks

Posted on 2004-09-07
15
245 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now