Will there be a performance penalty if you make a method synchronized? If so, can you make any design changes to improve the performance

Editorial / Best Answer

Answered by: Faisal Ghauri

  • Jul 24th, 2005


Performance does take a hit when using synchronization. I think the way to reduce this hit is to synchronize only a block of code that will be accessed by threads and not synchronize the entire method. 
 

Showing Answers 1 - 11 of 11 Answers

Jayashree C.H

  • Jun 27th, 2005
 

yes.the performance will be down if we use synchronization. 
one can minimise the penalty by including garbage collection algorithm, which reduces the cost of collecting large numbers of short- lived objects. and also by using Improved thread synchronization for invoking the synchronized methods.the invoking will be faster. 
 
regards 
Jayashree C.H

  Was this answer useful?  Yes

Faisal Ghauri

  • Jul 24th, 2005
 

Performance does take a hit when using synchronization. I think the way to reduce this hit is to synchronize only a block of code that will be accessed by threads and not synchronize the entire method. 
 

ganeshh

  • Mar 29th, 2007
 

It is very useful because we can make only one thread to access the resources at a time with this we can avoid deadlock situation.

  Was this answer useful?  Yes

The Synchronization may be useful in avoiding the deadlock, helpful in maintaining the consistent object state etc,. Only one thread will be accessed at a time by acquiring lock on the synchronized block or method etc,.

The Synchronization at in appropriate level, will definitely hampers the performance. 

One should strike a balance between the data sensitivity and performance.

Regards,
Srinivas Dasamanthula 

sumeesudha

  • Feb 5th, 2010
 

There is degradation in performance when you make a method synchronized.  Even when a program contains only a single thread running on a single processor, a synchronized method call is still slower than an unsynchronized method call. If the synchronization actually requires contending for the lock, the performance penalty is substantially greater, as there will be several thread switches and system calls required.

For a good design to improve the performance you must first identify what data will be shared across threads. If you are writing data that may be read later by another thread, or reading data that may have been written by another thread, then that data is shared, and you must synchronize when accessing it.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions