Java multithreaded programming using synchronized block synchronization method _java

Source: Internet
Author: User
Tags static class

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

Copy Code code as follows:

public void Method ()
{
... ...
Synchronized (expression)
{
... ...
}
}

Synchronization of non-static class methods

From the use of 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 that's not a good idea. And if the synchronized block is used 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 synchronous non-static methods in a class, you need to use this as the parameter for the synchronized block to pass in the synchronized block state with the following code:
Synchronization of non-static methods through synchronized blocks

Copy Code code as follows:

public class SyncBlock
{
public void Method1 ()
{
Synchronized (this)//equivalent to Method1 method using synchronized keyword
{
... ...
}
}
public void Method2 ()
{
Synchronized (this)//equivalent to METHOD2 method using synchronized keyword
{
... ...
}
}
Public synchronized void Method3 ()
{
... ...
}
}

Synchronized blocks are used in the Method1 and Method2 methods in the above code. The Method3 method on line No. 017 still uses the Synchronized keyword to define the method. When using the same SyncBlock class instance, the other two methods are blocked because they do not have a synchronized lock, as long as one of the three methods is executing. In order to achieve the same effect as the Synchronized keyword when using a synchronized block, all code must be written in a synchronized block, otherwise 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 for the synchronized block, Syncblock.this can be used as a parameter to the synchronized block to achieve the same effect.
When a synchronized block is used in a method of an inner class (Innerclass), this represents only the inner class, and the outer class (Outerclass) is not related. However, the non-static methods of the inner class can be synchronized with the non-static methods of the outer class. If you add a method4 method to the inner class Innerclass and synchronize the Method4 method with the SyncBlock three methods, the code is as follows:
Synchronize non-static methods of inner classes with non-static methods of outer classes

Copy Code code 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 synchronizes 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.
Synchronized block whether the normal execution, or because of a program error and exit synchronized block, the current synchronized block of the synchronized lock will automatically release. Therefore, you do not have to worry about the release of the sync lock when using the synchronized block.

Ii. synchronization of Static class methods
Because the object instance is not necessarily created when the static method is invoked. Therefore, you cannot use this to synchronize static methods, and you must use a class object to synchronize static methods. The code is as follows:
Synchronizing static methods through synchronized blocks

Copy Code code as follows:

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 when synchronizing static methods. In the above example, the Method1 and Method2 methods can only be executed in one way 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 previous example can be modified as follows:
Using the GetClass method to get the class object

Copy Code code as follows:

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

}
}
}

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

Copy Code code as follows:

public class Test
{
public static void Method ()
{
Synchronized (Staticsyncblock.class)
{

}
}
}

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

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.