GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 480 of 493    Print  
Synchronization in Java
When We should use Synchronization in Java and when we should not?


  
Total Answers and Comments: 7 Last Update: July 01, 2008     Asked by: sathyasha 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: vaneet
 
Synchronization is helpful only when you know that more than 1 thread is trying to access the same method. for that purpose, you wuill have to synchronize the method ie. only that thread which has the key for the lock(the synchronization lock) will be let inside. No other thread can access that method untill it is given the key

Above answer was rated as good by the following members:
damodarJ, pravinsharma
June 01, 2008 15:27:02   #1  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: Synchronization in Java
If u want to access the resource or method one at a time than we go for Synchronization.

To leads the deadlock

 
Is this answer useful? Yes | No
June 03, 2008 03:03:06   #2  
vaneet Member Since: July 2007   Contribution: 9    

RE: Synchronization in Java
Synchronization is helpful only when you know that more than 1 thread is trying to access the same method. for that purpose, you wuill have to synchronize the method ie. only that thread which has the key for the lock(the synchronization lock) will be let inside. No other thread can access that method untill it is given the key
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
June 14, 2008 13:39:20   #3  
interviewprep9 Member Since: June 2008   Contribution: 10    

RE: Synchronization in Java
Synchronization is useful when you have a shared  resource(say a shared variable) which is trying to be accessed by multiple threads.

Say you have two threads T1 and T2 and both want to access the variable 'a'. Now if T1 changes the value of 'a' from 1 to 2 and then T2 reads it but then T1 changes the value again now from 2 to back 1, what T2 read was a wrong value of 'a'. To avoid this, Locks are used. T1 has the lock now untill it finishes it's work with 'a' then releases the lock. T2 now will gain the lock and work on 'a'.


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
June 16, 2008 16:31:41   #4  
Uma Sundar Member Since: June 2008   Contribution: 2    

RE: Synchronization in Java
Synchronization means several methods can be excuted simultaniously using the same object.But if you want to excute one method at a time,that method can be declared as synchronized.For example,in the stack either the push or pop operation can be executed at one time,for that you declared both methods as synchronised.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
June 22, 2008 03:08:34   #5  
amandeep0183 Member Since: June 2008   Contribution: 1    

RE: Synchronization in Java
Synchronisation is the process of allowing only single thread to access field.In a scenario where value of field is changing there synchronisation is required to avoid inconsistencies else if field is constant then multiple threads can access that field at the same time.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
June 29, 2008 12:44:52   #6  
rinumca Member Since: June 2008   Contribution: 7    

RE: Synchronization in Java

Generally each thread will have its own resource or data to action. In this case there is no possibility for data inconsistency. Somtimes two threads may act on one resource of data and in this case data inconsistency may occur. To overcome this problem java introduces synchronization concept. In synchronization only one thread allowed to operate on the resource when a thread access a synchronized data it calls wait method, it is a indication to the thread schedulor not to allow any thread to access the same resource until the first thread job is completed. "synchronized" is a keyword which can be used to tell through method. We can make a method as synchronized and block as synchronized. We can use synchronized keyword to avoid inconsistency.


 
Is this answer useful? Yes | No
July 01, 2008 04:11:42   #7  
roopesh12 Member Since: July 2008   Contribution: 1    

RE: Synchronization in Java

There are two types of variables.
1. local variables
2. static or instance variables.

local valiables like method parameters are stored in stacks and instance are stored in Heap. You are not bothered to take care for any local things for synchronizations as that remains in thread's address. So two threads can never spoil the state that is in stack. So only thing that is in heap should be considered for synchronization. When you need some code to be atomic keep that in synchronized block. Like if you want to set r ,g, b colors in a method keep them in asynchronized block as the possibility is another thread will get partialy written data.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape