Top Companies hiring on Shine.com. Click Here to apply !
Click Here to Submit Your Resume for MNC Jobs !
JavaKen.com

Go Back   JavaKen.com > Java Introduction > Object Oriented Concepts
Forgot User Name / Password ?


Reply
 
Thread Tools Display Modes
  #1  
Old 09 December 2009, 09:34 AM
anoop.chiliveri anoop.chiliveri is offline
Junior Member
 
Join Date: 30 Oct 2009
Posts: 22
Default What is meant by Synchronization?

Click Here To Register & Submit Your Resume !

Could any one tell me What are the uses of synchronization?

Click Here To Register & Submit Your Resume !
Reply With Quote

  #2  
Old 09 December 2009, 10:43 AM
nadeshganesh nadeshganesh is offline
Junior Member
 
Join Date: 03 Apr 2009
Location: chennai
Posts: 12
Default Re: Synchronization

if more than one thread acting with same function then we can use synchronization keyword to that function.

Last edited by nadeshganesh; 09 December 2009 at 10:45 AM.
Reply With Quote
  #3  
Old 10 December 2009, 05:15 PM
avmaheedhar's Avatar
avmaheedhar avmaheedhar is offline
Junior Member
 
Join Date: 05 Oct 2008
Location: Bangalore
Posts: 24
Thumbs up Re: Synchronization

it is a capability to control access of multiple threads to common shared resources. Without this(sync), it is not possible for one thread to modify a shared object while another thread is in the process of using or updating that object's values.
Reply With Quote
  #4  
Old 10 December 2009, 11:40 PM
khadangap khadangap is offline
Member
 
Join Date: 21 Jun 2009
Location: Marathali,Bangalore
Posts: 66
Default Re: Synchronization

Hi Anoop,

How are you. And how your interviews are going on ?
I am trying to answer you.
  1. Synchronization is nothing but thread safe .
    Synchronization is meant to prevent the access of more then one thread to a particular method / object .
    It comes under two flavours synchronizing a method or synchronizing an object .
Synchornizing a method:
to synchronize a method means no other Thread can enter to this method if one thread has already enters to this method . second Thread is bound to wait till the first Thread relise its lock.
Syntax:
Synchronized method(){.......}
Synchronizing an object:
By making an object sychronized no other Thread can access the instance
till the first Thread releases the lock.Again this comes under two flavour as
  • synchronizing this . Syntax: Synchronized(this){......}
    synchronizing an object . Syntax: Synchronized(object){......}
If u tell this again the interviewer will ask u when to use synchronized block and when to use synchronized method ? and be prepared for that .Don't mind
Reply With Quote
  #5  
Old 18 December 2009, 03:42 PM
DeeptiSaraf DeeptiSaraf is offline
Junior Member
 
Join Date: 23 Aug 2009
Posts: 2
Wink Re: Synchronization

Quote:
Originally Posted by anoop.chiliveri View Post
Could any one tell me What are the uses of synchronization?
Synchronization:-

Mutual Exclusion(At a one time only one thread can access the resources in critical section).After \completion of that thread other thread will start the execution.

State in Synchronization::

Wait
Notify
Notifyall
Reply With Quote
  #6  
Old 19 December 2009, 06:19 PM
naveenm_006 naveenm_006 is offline
Member
 
Join Date: 16 Sep 2008
Posts: 87
Default Re: Synchronization

Hi Khadangap,

Nice Explanation

Could you plz elobrate with an example in which situation we use Synchronizing a method and synchronizing an object? ie., real time senario

Also tell me the difference b/w Synchronized block and Synchronized Method.

Regards,
Naveen.
Reply With Quote
  #7  
Old 20 December 2009, 11:32 PM
khadangap khadangap is offline
Member
 
Join Date: 21 Jun 2009
Location: Marathali,Bangalore
Posts: 66
Default Re: Synchronization

Hi naveen,
please find your ans:

The addition of the synchronised modifier to a method declaration ensures that only one thread is allowed inside the method at a time . This can be useful in keeping out other threads while the state of an object is temporarily inconsistant .
the Synchronised method look like this :
Public synchronised void method{}
The synchronised block can be used when a whole method doesn't need to be synchronised or when u want the thread to get an object level lock on a different object .
the Synchronised block look like this :
synchronised(obj){
//block of code
}
Analysis :in web application we know context attribute is not thread safe . How do we make a context attribute Thread safe and what happen when we use synchronised method or synchronised block
when we write the following code
EXAMPLE
public synchronised void doGet(HttpServletRequest req,HttpServletResponse res )throws IOException,ServletException{
req.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("test context attributes");
getServletContext().setAttribute("foo","22");
getServletContext().setAttribute("bar","42");
out.println(getServletContext().getAttribute("foo" ));
out.println(getServletContext().getAttribute("bar" ));// Here u may get different result because anyotherservlet can change it since context attribute are accessable to all in a particula application.
);
By making this doGet()(Which is a bed idea) we can prevent multiple thread to access the service method of the above servlet but we cannot prevent the context attribute being accessed by other servlet bcz context attribute are not one for a particular servlet . Any servlet can change the attribute by calling setAttribute() method so the state is not consistent which wll give us wrong data .
Rescue:we required context attribute to make thread safe not service method bcz by making service method(doGet in this case )thread safe we are breaking the rule of concorrent access of service method .
so for this purpose go through the following code which donnt break the rule of servlet as well as we can make context attribute thread safe .
EXAMPLE
public void doGet(HttpServletRequest req,HttpServletResponse res )throws IOException,ServletException{
req.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("test context attributes");
synchronised(getServletContext()){
getServletContext().setAttribute("foo","22");
getServletContext().setAttribute("bar","42");
out.println(getServletContext().getAttribute("foo" ));
out.println(getServletContext().getAttribute("bar" ));
}
);

By writing synchronised(getServletContext()){-------}
we make the context object thread safe that means when we enter this block it holds a luck on context object and no body can change the context attribute when we enter to this block .
conclusion
sorry for the big answer . from the above we concluded that when we required a lock on a particular object insted of method with some extra code execution in tne method(in the above example 1st three line of codes )we have to use synchronised block .
Thanks & regards
pradyumna khadanga
Reply With Quote
  #8  
Old 27 December 2009, 07:48 PM
nelavellinarendra's Avatar
nelavellinarendra nelavellinarendra is offline
Junior Member
 
Join Date: 13 Dec 2009
Location: hyderabad
Posts: 4
Default Re: Synchronization

[QUOTE=anoop.chiliveri;7372]Could any one tell me What are the uses of synchronization?
hi anup this is naresh from hyderabad
i would like to tell the synchronization use:=

first we concentrate on what is synchronization ok
ans synchronization is a mechanism through which we get the thread safety.
or
synchronization is a mechanization which allows only one thread at a time to access the business logic or the logic which we have mentioned in the synchronization method or block.
now where we can use this synchronization technic

simple when we feel that the business logic will be accessed by multiple users and that too where logic is very sensitive logic like withdrawing money,and ticket reservations in those situation the business logic is very sensitive and there is chance of accessing the same business logic one or more threads at time so in those situations we can go for synchronization concept.But remember some times it causes to deadlock problem.
for any clarification contact
naresh nelavelli
narez.thread@hotmail.com
9700241488.
all the best
Reply With Quote
  #9  
Old 29 March 2010, 03:02 PM
suneetha211 suneetha211 is offline
Junior Member
 
Join Date: 28 Mar 2010
Posts: 19
Default Re: Synchronization

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources.
Without synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable.
This usually leads to significant errors.
Reply With Quote
  #10  
Old 14 April 2010, 10:44 AM
123venkat 123venkat is offline
Junior Member
 
Join Date: 14 Dec 2009
Posts: 5
Default Re: Synchronization

Whats is difference between synchronized block and synchronized Method?

excuese my ignorance I have read some where sychronized key word can only be used with methods? Then wats the synch block??
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between making the method synchronization and synchronization block ? raja.careers Threads 6 08 October 2011 02:15 PM
What is Synchronization and why is it important ? SoCoooollllllllllllllllll Threads 1 20 February 2009 12:45 PM
What is Reentrant Synchronization ? vbraojava Threads 0 01 October 2008 05:13 PM


All times are GMT +5.5. The time now is 05:25 AM.


Powered by vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 2008 ChetanaS, All Rights Reserved.