Synchronized Block v.s. Synchronized Method

Discussion in 'Java' started by Jerry, Jun 17, 2005.

  1. Jerry

    Jerry Guest

    Anyone knows the differences between synchronized block and synchronized method?

    Thanks!
     
    Jerry, Jun 17, 2005
    #1
    1. Advertising

  2. On 16 Jun 2005 21:52:02 -0700, Jerry wrote:

    > Anyone knows the differences between synchronized block and synchronized method?


    Yep.

    > Thanks!


    No worries.

    BTW Jerry - do you mind trying to find these answers in the
    books you were assigned for the course (or notes, handouts etc.)
    rather than ask people on usenet to do your work/thinking for you?
    <http://www.physci.org/codes/javafaq.jsp#homework>

    As an aside, a better group for those learning Java is
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jun 17, 2005
    #2
    1. Advertising

  3. (Jerry) writes:

    > Anyone knows the differences between synchronized block and synchronized method?


    A synchronized method is a method with an implicit

    synchronized (this) {

    }

    around all statements.

    (or synchronized(MyClass.class) for static methods)

    The difference is that an explicit synchronized block can span any
    number of method statements, and can use any object for synchronizing
    on.
     
    Tor Iver Wilhelmsen, Jun 17, 2005
    #3
  4. Jerry

    nicemothi

    Joined:
    Nov 3, 2007
    Messages:
    1
    Synchronized block Vs Synchronized method

    First of all to achieve Multithreading mechanism in java we should go for synchronization. And this can be done in two ways depending on the requirement.

    1. Synchronized block and
    2. Synchronized method.

    if you go for synchronized block it will lock a specific object.

    if you go for synchronized method it will lock all the objects.

    in other way Both the synchronized method and block are used to acquires the lock for an object. But the context may vary. Suppose if we want to invoke a critical method which is in a class whose access is not available then synchronized block is used. Otherwise synchronized method can be used.

    Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc

    For a detail clarification see the below code

    for example:
    //Synchronized block

    class A
    { public void method1() {...} }
    class B
    {
    public static void main(String s[])
    { A objecta=new A();
    A objectb=new A();
    synchronized(objecta){objecta.method1();}
    objectb.method1(); //not synchronized
    }
    }


    //synchronized method
    class A
    { public synchronized void method1() { ...}
    }
    class B
    {
    public static void main(String s[])
    {
    A objecta=new A();
    A objectb =new A();
    objecta.method1(); objectb.method2();

    }
    }



    Moti Lal D
     
    nicemothi, Nov 3, 2007
    #4
  5. Jerry

    tonni

    Joined:
    Aug 11, 2010
    Messages:
    1
    Synchronized blocks

    Synchronized blocks are quite useful also for many other situations. For example you can prevent a nested monitor problem where the code would otherwise go deadlock. In general they are a good way to separate code that needs synchronization from other stuff, which can sometimes speed up the program.
     
    tonni, Aug 11, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    6
    Views:
    29,364
  2. dmcreyno
    Replies:
    9
    Views:
    9,622
    Mark Space
    Jun 27, 2006
  3. morrell
    Replies:
    1
    Views:
    999
    roy axenov
    Oct 10, 2006
  4. ankur
    Replies:
    4
    Views:
    1,506
    Eric Sosman
    Nov 28, 2008
  5. Kyung won Cheon
    Replies:
    0
    Views:
    230
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page