What is meant by Synchronization?

Discussion in 'Object Oriented Concepts' started by anoop.chiliveri, Dec 9, 2009.

  1. anoop.chiliveri

    anoop.chiliveri New Member

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

  2. nadeshganesh

    nadeshganesh New Member

    Re: Synchronization

    if more than one thread acting with same function then we can use synchronization keyword to that function.
     
    Last edited: Dec 9, 2009
  3. avmaheedhar

    avmaheedhar New Member

    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.
     
  4. khadangap

    khadangap New Member

    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
     
  5. DeeptiSaraf

    DeeptiSaraf New Member

    Re: 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
     
  6. naveenm_006

    naveenm_006 New Member

    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.
     
  7. khadangap

    khadangap New Member

    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
     
  8. nelavellinarendra

    nelavellinarendra New Member

    Re: Synchronization

     
  9. suneetha211

    suneetha211 New Member

    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.
     
  10. 123venkat

    123venkat New Member

    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??
     
  11. clausbelly

    clausbelly New Member

    Re: Synchronization

    if you have ssh setup to the box, you can configure your navicat connection to tunnel through ssh. this way you don't have to mess with your mysql permissions. I am assuming that once the dual synchronization is set up that it's automatic and users don't have to do anything..
     

Share This Page