Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

后端 未结 23 1971
北荒
北荒 2020-11-22 04:29

Can any one tell me the advantage of synchronized method over synchronized block with an example?

相关标签:
23条回答
  • 2020-11-22 05:00

    With synchronized blocks, you can have multiple synchronizers, so that multiple simultaneous but non-conflicting things can go on at the same time.

    0 讨论(0)
  • 2020-11-22 05:01

    Important note on using the synchronized block: careful what you use as lock object!

    The code snippet from user2277816 above illustrates this point in that a reference to a string literal is used as locking object. Realize that string literals are automatically interned in Java and you should begin to see the problem: every piece of code that synchronizes on the literal "lock", shares the same lock! This can easily lead to deadlocks with completely unrelated pieces of code.

    It is not just String objects that you need to be careful with. Boxed primitives are also a danger, since autoboxing and the valueOf methods can reuse the same objects, depending on the value.

    For more information see: https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused

    0 讨论(0)
  • 2020-11-22 05:02

    Synchronized method is used for lock all the objects Synchronized block is used to lock specific object

    0 讨论(0)
  • 2020-11-22 05:04

    From a Java specification summary: http://www.cs.cornell.edu/andru/javaspec/17.doc.html

    The synchronized statement (§14.17) computes a reference to an object; it then attempts to perform a lock action on that object and does not proceed further until the lock action has successfully completed. ...

    A synchronized method (§8.4.3.5) automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the lock associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the lock associated with the Class object that represents the class in which the method is defined. ...

    Based on these descriptions, I would say most previous answers are correct, and a synchronized method might be particularly useful for static methods, where you would otherwise have to figure out how to get the "Class object that represents the class in which the method was defined."

    Edit: I originally thought these were quotes of the actual Java spec. Clarified that this page is just a summary/explanation of the spec

    0 讨论(0)
  • 2020-11-22 05:05

    The only real difference is that a synchronized block can choose which object it synchronizes on. A synchronized method can only use 'this' (or the corresponding Class instance for a synchronized class method). For example, these are semantically equivalent:

    synchronized void foo() {
      ...
    }
    
    void foo() {
        synchronized (this) {
          ...
        }
    }
    

    The latter is more flexible since it can compete for the associated lock of any object, often a member variable. It's also more granular because you could have concurrent code executing before and after the block but still within the method. Of course, you could just as easily use a synchronized method by refactoring the concurrent code into separate non-synchronized methods. Use whichever makes the code more comprehensible.

    0 讨论(0)
  • 2020-11-22 05:05

    Synchronized Method

    Pros:

    • Your IDE can indicate the synchronized methods.
    • The syntax is more compact.
    • Forces to split the synchronized blocks to separate methods.

    Cons:

    • Synchronizes to this and so makes it possible to outsiders to synchronize to it too.
    • It is harder to move code outside the synchronized block.

    Synchronized block

    Pros:

    • Allows using a private variable for the lock and so forcing the lock to stay inside the class.
    • Synchronized blocks can be found by searching references to the variable.

    Cons:

    • The syntax is more complicated and so makes the code harder to read.

    Personally I prefer using synchronized methods with classes focused only to the thing needing synchronization. Such class should be as small as possible and so it should be easy to review the synchronization. Others shouldn't need to care about synchronization.

    0 讨论(0)
提交回复
热议问题