Java multithreaded programming using synchronized block synchronization method

Source: Internet
Author: User

Synchronized block to synchronize an object variable, you can also use the synchronized block to synchronize static and non-static methods in the class, using the synchronized block synchronization method below

There are two uses of the Synchronized keyword. The first is used directly in the definition of a method as described in the article "using the Synchronized Keyword Synchronization class method". The other is the synchronized block. Not only can we synchronize an object variable with the synchronized block. You can also use synchronized blocks to synchronize static and non-static methods in a class.
The syntax for the synchronized block is as follows:

Copy CodeThe code is as follows:
public void Method ()
{
... ...
Synchronized (expression)
{
... ...
}
}

Synchronization of non-static class methods

From the article "using the Synchronized Keyword Synchronization class method" We know that using the Synchronized keyword to define a method locks all static or non-static methods defined in the class using the Synchronzied keyword, but this is not a good idea. If you use synchronized blocks to achieve the same effect, it is not difficult to understand why this effect is produced. If you want to use the synchronized block to lock all of the synchronous non-static methods in the class, you need to use this as a parameter for the synchronized block to pass in the synchronized block country with the following code:
Synchronous non-static method via synchronized block

Copy CodeThe code is as follows:
public class SyncBlock
{
public void Method1 ()
{
Synchronized (this)//is equivalent to using the Synchronized keyword for the method1 method
{
... ...
}
}
public void Method2 ()
{
Synchronized (this)//is equivalent to using the Synchronized keyword for the Method2 method
{
... ...
}
}
Public synchronized void Method3 ()
{
... ...
}
}

The synchronized block is used in the Method1 and Method2 methods in the preceding code. The Method3 method in line No. 017 still uses the Synchronized keyword to define the method. When you use the same SyncBlock class instance, the other two methods are blocked by not acquiring a synchronization lock as long as one of the three methods is executing. To achieve the same effect as the Synchronized keyword when using the synchronized block, all the code must be written in the synchronized block, otherwise all the code in the current method will not be synchronized with the other methods.
In addition to using this as a synchronized block parameter, you can also use Syncblock.this as the parameter of the synchronized block to achieve the same effect.
When a synchronized block is used in an inner class (Innerclass) method, this only represents the inner class, and the outer class (Outerclass) has no relationship. But the non-static method of the inner class can be synchronized with the non-static method of the outer class. If you add a method4 method to the inner class Innerclass and synchronize the Method4 method with the three methods of SyncBlock, the code is as follows:
Synchronizing non-static methods of inner classes with non-static methods of outer classes

Copy CodeThe code is as follows:
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 class is synchronized with the other three methods of the SyncBlock class, so method1, METHOD2, Method3 and METHOD4 Four methods can only have one method at a time to execute.
Synchronized block regardless of whether the normal execution, or because the program error and unexpectedly exited the synchronized block, the current synchronized block holds the synchronization lock will be automatically released. Therefore, you do not have to worry about the release of the sync lock when using synchronized blocks.

Second, the synchronization of static class methods
Because the 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 class objects to synchronize static methods. The code is as follows:
Synchronous static method via synchronized block

Copy CodeThe code is as follows:
public class Staticsyncblock
{
public static void Method1 ()
{
Synchronized (Staticsyncblock.class)
{
... ...
}
}
public static synchronized void Method2 ()
{
... ...
}
}


When synchronizing static methods, you can use the class's static field class to get the class object. In the above example, the Method1 and Method2 methods can only have one method execution at a time. In addition to using the class field to get the class object, you can also use the GetClass method of the instance to get the class object. The code in the example above can be modified as follows:
Using the GetClass method to get the class object

Copy CodeThe code is as follows:
public class Staticsyncblock
{
public static Staticsyncblock instance;
Public Staticsyncblock ()
{
instance = this;
}
public static void Method1 ()
{
Synchronized (Instance.getclass ())
{

}
}
}

In the above code, an instance of the Staticsyncblock class is obtained through a static instance of public. The class object is obtained by the GetClass method of this example (all instances of a class are given the same class object through the GetClass method, so the GetClass method of invoking any instance) is possible. We can also synchronize static methods of different classes with class objects, such as the static method of the test class and the two static methods of the Staticsyncblock class, with the following code:
The method of the test class is synchronized with the Method1 and Method2 methods of the Staticsyncblock class

Copy CodeThe code is as follows:
public class Test
{
public static void Method ()
{
Synchronized (Staticsyncblock.class)
{

}
}
}

Note: When using the Synchronized block synchronization class method, non-static methods can use this to synchronize, whereas static methods must be synchronized using the class object. They do not affect each other. Of course, you can also use class objects in non-static methods to synchronize static methods. However, you cannot use this in a static method to synchronize a non-static method. This should be noted when using the Synchronized block synchronization class method.

Java multithreaded programming using synchronized block synchronization method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.