GeekInterview.com
Series: Subject: Topic:
Question: 563 of 573

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

Asked by: Interview Candidate | Asked on: Jul 25th, 2005

Editorial / Best Answer

Answered by: Faisal Ghauri

Answered On : 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 - 5 of 5 Answers
Jayashree C.H

Answered On : 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

  
Login to rate this answer.
Faisal Ghauri

Answered On : 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. 
 

Yes  5 Users have rated as useful.
  
Login to rate this answer.
ganeshh

Answered On : 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.

  
Login to rate this answer.
sripatnaiky

Answered On : Feb 4th, 2010

View all answers by sripatnaiky

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 

Yes  1 User has rated as useful.
  
Login to rate this answer.
sumeesudha

Answered On : Feb 5th, 2010

View all answers by sumeesudha

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.