Example of a simple synchronization operation using the Synchronized keyword in Java _java

Source: Internet
Author: User
Tags modifier

A simple record of how to use the Synchronized keyword in java.

Before you introduce, you need to be clear that the object instance of each class in Java has a single lock (lock) associated with it, and that the Synchronized keyword only works on that lock, that is, you can assume that synchronized only functions on the object instance of the Java class.

Synchronized modifier function

Copy Code code as follows:

Public synchronized Amethod () {
}

This is the most commonly used scenario, so what is the purpose of this synchronization method, for the convenience of the Amethod method is recorded.

1, synchronized lock is called the synchronization method object instance, for example, the same instance P1 in different threads are called Amethod will produce synchronization;
2, note that this object belongs to the class of another object P2 can call this amethod arbitrarily, because the synchronized method of different object instances is not interference. That is, other threads can still access the Amethod method in another object instance of the same class at the same time;
3, if an object has multiple synchronized methods, such as Amethod, Bmethod, Cmethod, now as long as one thread accesses one of the synchronized methods, Other threads cannot access any one of the synchronized methods in this object at the same time.

The above code is actually equivalent to the following:

Copy Code code as follows:

public void Amethod () {
Synchronized (this) {
}
}

This is the reference to the instance object, such as P1. The visible synchronization method is essentially a synchronized action on the object reference. The thread that got the lock on the P1 object was able to invoke the P1 synchronization method, and for P2, P1 the lock had nothing to do with him, and the program could get rid of the synchronization mechanism in this situation, resulting in data chaos. This leads us to the following synchronization block.

Synchronized Cosmetic code block

Copy Code code as follows:

public void Dmethod (Someobject so) {
Synchronized (SO) {
}
}

Here synchronized gets the lock that is the object of so, and whoever gets the lock will be able to run the code he controls. When there is a clear object as a lock, it can write the program, but when there is no clear object as a lock, just want to let a piece of code synchronized, can create a special instance variable (he must be an object) to act as a lock:
Copy Code code as follows:

Class Foo implements Runnable {
Private byte[] lock = new Byte[0];
public void Method () {
Synchronized (lock) {
}
}
}

A 0-length byte array object is more cost-effective than any object.

Synchronized Modified static method

As mentioned earlier, the Synchronized keyword is valid only for P1 instances in different threads, so how can you work with different instances of P1 and P2 at the same time, the answer is to use the synchronized modifier static method, the class static method can be said that the class has its own, does not rely on the class instance, So as long as we use the Synchronized keyword to modify the static method of the class, we can achieve synchronization between the different instances.

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.