Java multithreaded programming USES the Synchronized block synchronization method

  • 2020-04-01 02:50:05
  • OfStack

The synchronized keyword is used in two ways. The first is directly used in the definition of a method, as described in the article using the Synchronized keyword to synchronize class methods. The other is synchronized blocks. We can synchronize an object variable with more than just a synchronized block. You can also use synchronized blocks to synchronize static and non-static methods in a class.
The syntax for synchronized blocks is as follows:


public void method()
{
     ...   ... 
    synchronized( expression )
    {
         ...   ... 
    }
}

Synchronization & synchronization of non-static class methods;

We know from using the Synchronized keyword to synchronize class methods that using the Synchronized keyword to define a method locks all static or non-static methods in the class defined using the synchronzied keyword, but this is not easy to understand. If you use synchronized blocks to achieve the same effect, it's not hard to see why. If you want to use a synchronized block to lock all synchronized non-static methods in a class, you need to pass this into the synchronized block country as an argument to the synchronized block. The code is as follows:
Synchronization of nonstatic methods through synchronized blocks


public class SyncBlock
   {
       public void method1()
       {
           synchronized(this)  //This is equivalent to using the synchronized keyword for method1 methods
           {
                ...   ... 
           }
       }
       public void method2()
       {
           synchronized(this)  //This is equivalent to using the synchronized keyword for method2 methods
           {
                ...   ... 
           }
       }
       public synchronized void method3()  
       {
            ...   ... 
       }
   }

Synchronized blocks are used in the method1 and method2 methods in the code above. The method3 method on line 017 still USES the synchronized keyword to define the method. When using the same SyncBlock class instance, as long as one of these three methods is executing, the other two methods are blocked because they do not acquire a synchronization lock. To have the same effect as the synchronized keyword when using synchronized blocks, you must write all the code in a synchronized block, or you will not be able to synchronize all the code in the current method with other methods.
In addition to using this as an argument to a synchronized block, you can use syncblock. this as an argument to a synchronized block to achieve the same effect.
When synchronized blocks are used in methods of the InnerClass, this represents only the InnerClass, not the OuterClass. But non-static methods of inner classes can be synchronized with non-static methods of outer classes. Add a method4 method to the InnerClass InnerClass and synchronize the method4 method with the three methods of SyncBlock as follows:
Synchronize non-static methods of inner classes with non-static methods of outer classes


public class SyncBlock
{
     ...   ... 
    class InnerClass
    {
        public void method4()
        {
            synchronized(SyncBlock.this)
            {
                 ...   ...  
            }
        }
    }
     ...   ... 
}

In the new version of the SyncBlock class above, the method4 method of the InnerClass is synchronized with the other three methods of the SyncBlock class, so that only one of the four methods -- method1, method2, method3, and method4 -- can execute at the same time.
Whether a Synchronized block completes its normal execution or exits due to a program error, the Synchronized lock held by the current Synchronized block is automatically released. Therefore, you do not have to worry about the release of synchronized locks when using synchronized blocks.

Synchronization of static class methods
Because an object instance is not necessarily created when a static method is called. Therefore, you cannot use this to synchronize static methods, but you must use the Class object to synchronize static methods. The code is as follows:
A static method is synchronized through a synchronized block


public class StaticSyncBlock
{
       public static void method1()
       {
           synchronized(StaticSyncBlock.class)  
           {
                ...   ... 
           }
       }
       public static synchronized void method2()  
       {
            ...   ... 
       }
   }

You can use the class's static field class to get the class object while synchronizing static methods. In the example above, method1 and method2 methods can only have one method executed at the same time. In addition to using the class field to get the class object, you can use the instance's getClass method to get the class object. The code in the above example can be modified as follows:
Use the getClass method to get the Class object


public class StaticSyncBlock
{
    public static StaticSyncBlock instance; 
    public StaticSyncBlock()
    {
        instance = this;
    }
    public static void method1()
    {
       synchronized(instance.getClass())
       {

       }
    }   
}

In the above code, we get an instance of the StaticSyncBlock Class through a public static instance, and get the Class object through the instance's getClass method (all instances of a Class get the same Class object through the getClass method, so we can call any instance's getClass method). We can also synchronize static methods of different classes through Class objects, such as the static method of Test Class and the two static methods of StaticSyncBlock Class. The code is as follows:
Method method of Test class is synchronized with method1 and method2 methods of StaticSyncBlock class


public class Test
   {
       public static void method()
       {
           synchronized(StaticSyncBlock.class)
           {

           }
       }
   }

Note: when using Class methods with synchronized blocks, non-static methods can use this to synchronize, while static methods must use Class objects to synchronize. They don't affect each other. Of course, you can also use Class objects in non-static methods to synchronize static methods. But you cannot use this to synchronize non-static methods in static methods. This should be noted when using synchronized blocks to synchronize class methods.


Related articles: