Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35577

Thread Safety Vs Performance

I am deciding what is the best way to achieve high performance gain while achieving thread safety (synchronization) for required point. Consider the following case. There are two entry point in system and I want to make sure there is no two or more threads updates cashAccounts and itemStore at same time. So I created a Object call Lock and use it as follows.

public class ForwardPath {
public void fdWay(){
    synchronized (Lock.class){
        //here I am updating both cashAccount object and
        //itemStore object 
    }              
  }
}

.

public class BackWardPath {
public void bwdWay(){
   synchronized (Lock.class){
       //here I am updating both cashAccount object and
       //itemStore object
   }
  }  
}

But this implementation will greatly decrease performance, If both ForwardPath and BackWardPath are triggered frequently.

But in this case it is some what difficult to lock only cashAccount and itemStore because both these objects get updates several times inside both paths.

Is there a good way to achieve both performance gain and thread safety in this scenario ?

Upvotes: 0

Views: 446

Answers (2)

TwoThe
TwoThe

Reputation: 14309

Performance-gain by using Threads is an architectural question, just adding some Threads and synchronized won't do the trick and usually just screws up your code while not working any faster than before. Therefore your code example is not enough to help you on the actual problem you seem to be facing, as each threaded solution is unique to your actual code.

Upvotes: 0

Durandal
Durandal

Reputation: 20069

The example is far too abstract, and the little you describe leaves no alternative to synchronization in the methods.

To obtain high scalability (thats not necessarily highest performance in all situations, mind you), work is usually subdivided into units of work that are completely independent of each other (these they can be processed without any synchronization).

Lets assume a simple example, summing up numbers (purely to demonstrate the principle):

The naive solution would be to have one accumulator for the sum, and walk the numbers adding to the accumulator. Obviously, if you wanted to use multiple threads, the accumulator would need to be synchronized and become the major point of contention).

To eliminate the contention, you can partition the numbers into multiple slices - separate units of work. Each unit of work can be summed independently (one thread per unit of work, for example). To get the final sum, add up the partial sums of each unit of work. The only point where synchronization is now needed is when combining the partial results. If you had for example 10 billion numbers, and divide them into 10 units of work, you need only synchronized 10 times - instead of 10 billion times in the naive solution.

The principle is always the same here: Make sure you can do as much work as possible without synchronization, then combine the partial results to obtain the final result. Thinking on the individual operation level is to fine a granularity to lend itself well to multi threading.

Upvotes: 1

Related Questions