What does 'synchronized' mean?

后端 未结 17 1626
广开言路
广开言路 2020-11-22 00:13

I have some questions regarding the usage and significance of the synchronized keyword.

  • What is the significance of the synchronized
17条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 00:36

    Synchronized normal method equivalent to Synchronized statement (use this)

    class A {
        public synchronized void methodA() {
            // all function code
        }
    
        equivalent to
    
        public void methodA() {
            synchronized(this) {
                 // all function code
            }
        } 
    }
    

    Synchronized static method equivalent to Synchronized statement (use class)

    class A {
        public static synchronized void methodA() {
            // all function code
        }
    
        equivalent to
    
        public void methodA() {
            synchronized(A.class) {
                 // all function code
            }
        } 
    }
    

    Synchronized statement (using variable)

    class A {
        private Object lock1 = new Object();
    
        public void methodA() {
            synchronized(lock1 ) {
                 // all function code
            }
        } 
    }
    

    For synchronized, we have both Synchronized Methods and Synchronized Statements. However, Synchronized Methods is similar to Synchronized Statements so we just need to understand Synchronized Statements.

    => Basically, we will have

    synchronized(object or class) { // object/class use to provides the intrinsic lock
       // code 
    }
    

    Here is 2 think that help understanding synchronized

    • Every object/class have an intrinsic lock associated with it.
    • When a thread invokes a synchronized statement, it automatically acquires the intrinsic lock for that synchronized statement's object and releases it when the method returns. As long as a thread owns an intrinsic lock, NO other thread can acquire the SAME lock => thread safe.

    => When a thread A invokes synchronized(this){// code 1} => all the block code (inside class) where have synchronized(this) and all synchronized normal method (inside class) is locked because SAME lock. It will execute after thread A unlock ("// code 1" finished).

    This behavior is similar to synchronized(a variable){// code 1} or synchronized(class).

    SAME LOCK => lock (not depend on which method? or which statements?)

    Use synchronized method or synchronized statements?

    I prefer synchronized statements because it is more extendable. Example, in future, you only need synchronized a part of method. Example, you have 2 synchronized method and it don't have any relevant to each other, however when a thread run a method, it will block the other method (it can prevent by use synchronized(a variable)).

    However, apply synchronized method is simple and the code look simple. For some class, there only 1 synchronized method, or all synchronized methods in the class in relevant to each other => we can use synchronized method to make code shorter and easy to understand

    Note

    (it not relevant to much to synchronized, it is the different between object and class or none-static and static).

    • When you use synchronized or normal method or synchronized(this) or synchronized(non-static variable) it will synchronized base on each object instance.
    • When you use synchronized or static method or synchronized(class) or synchronized(static variable) it will synchronized base on class

    Reference

    https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

    Hope it help

提交回复
热议问题