Use synchronized block Synchronization Method

Source: Internet
Author: User

The synchronized keyword can be used in two ways. The first is the definition of the method that is directly used in the article "Synchronize class methods with synchronized keywords. The other is the synchronized block. We can not only use the synchronized block to synchronize an object variable. You can also use the synchronized block to synchronize static and non-static methods in the class.

Synchronized block Syntax:

Public void method ()
{
... ...
Synchronized (expression)
{
... ...
}
}

1. Non-static method Synchronization

From the synchronized keyword synchronization class method article, we know that using the synchronized keyword to define a method will lock all static or non-static methods defined using the synchronzied keyword in the class, but this is hard to understand. If synchronized blocks are used to achieve the same effect, it is not difficult to understand why this effect occurs. To use the synchronized block to lock all the synchronous non-static methods in the class, use this as a parameter of the synchronized block to pass in the synchronized block country. The Code is as follows:

Use synchronized block to synchronize non-static methods

001 public class syncblock
002 {
003 public void Method1 ()
004 {
005 synchronized (this) // equivalent to using the synchronized keyword for the Method1 Method
006 {
007... ...
008}
009}
010 public void method2 ()
011 {
012 synchronized (this) // equivalent to using the synchronized keyword for the method2 Method
013 {
014... ...
015}
016}
017 public synchronized void method3 ()
018 {
019... ...
020}
021}

In the above Code, the Method1 and method2 methods use the synchronized block. The method3 method of Row 3 still uses the synchronized keyword to define the method. When the same syncblock class instance is used, as long as one of the three methods is being executed, the other two methods will be blocked because no synchronization lock is obtained. To achieve the same effect as the synchronized keyword when using the synchronized block, you must write all the code in the synchronized block. Otherwise, all code in the current method cannot be synchronized with other methods.

In addition to using this as the parameter of the synchronized block, you can also use syncblock. This as the parameter of the synchronized block to achieve the same effect.

When synchronized blocks are used in the innerclass method, this only indicates the inner class, and it has no relationship with the outerclass. However, non-static methods of internal classes can be synchronized with non-static methods of external classes. For example, add a method4 method to innerclass and synchronize the three methods of method4 and syncblock. The Code is as follows:

Synchronize non-static methods of internal classes with non-static methods of external 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 class is synchronized with the other three methods of the syncblock class. Therefore, the four methods of Method1, method2, method3, and method4 can only be executed by one method at a time.

Whether the synchronized block is successfully executed or unexpectedly exited due to a program error, the synchronization lock held by the current synchronized block is automatically released. Therefore, you do not have to worry about releasing synchronization locks when using synchronized blocks.

2. Static Method Synchronization

When a static method is called, the object instance is not necessarily created. Therefore, you cannot use this to synchronize static methods. Instead, you must use class objects to synchronize static methods. The Code is as follows:

Use synchronized block to synchronize static data

Public class staticsyncblock
{
Public static void Method1 ()
{
Synchronized (staticsyncblock. Class)
{
... ...
}
}
Public static synchronized void method2 ()
{
... ...
}
}

When synchronizing static methods, you can use the class static field to obtain the class object. In the preceding example, the Method1 and method2 methods can only be executed by one method at the same 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 previous example can be modified as follows:

Use the getclass method to obtain the Class Object

Public class staticsyncblock
{
Public static staticsyncblock instance;
Public staticsyncblock ()
{
Instance = this;
}
Public static void Method1 ()
{
Synchronized (instance. getclass ())
{

}
}

}

In the code above, a public static instance is used to obtain an instance of the staticsyncblock class, and get the class object through the getclass method of this instance (all instances of a class get the same class object through the getclass method. Therefore, call the getclass method of any instance ). We can also synchronize static methods of different classes through class objects, such as static methods of the test class and static methods of the staticsyncblock class. The Code is as follows:

The method of the test class is synchronized with the Method1 and method2 methods of the staticsyncblock class.

Public class test
{
Public static void method ()
{
Synchronized (staticsyncblock. Class)
{

}
}
}

Note: when using the synchronized block synchronization class method, you can use this for non-static methods, while the static method must use class objects for synchronization. They do not affect each other. Of course, you can also use class objects in non-static methods to synchronize static methods. However, this cannot be used in static methods to synchronize non-static methods. Note This when using the synchronized block synchronization class method.

 

From: http://blog.csdn.net/nokiaguy/article/details/4684706

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.