The Synchronized keyword in Java

Source: Internet
Author: User

Reference: http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html

The root cause of the multithreading problem:

In a multithreaded environment, when a change is made to an object, a thread a changes the variable, but does not change the finish, and the CPU is snatched by another thread B, then a is no longer executed, resulting in inconsistent data behavior. For the above citation in the bank withdrawal deposit example, originally saved 100 to 100 just offset, but because of the wanton preemption between the multithreading, some of the operation has not completed the deposit, naturally resulting in strange results.

Key points:

It is important to understand that synchronized is an object, not a code snippet, that is locked synchronously.

Each object has only one lock associated with it.

Synchronized field of action:

(1) keyword synchronized used before the method, similar to synchronized Amethod () {}, is actually the instantiation of object a plus lock.

(1.1) It is possible to prevent multiple threads from accessing the Synchronized method of this object A at the same time, and if there is another thread to access the method, wait
(1.2) If object A has more than one synchronized method, as long as one thread accesses one of the synchronized methods, the other thread cannot access any of the synchronized methods in the object at the same time.
(1.3) When a thread accesses a synchronized (this) synchronization code block of a, another thread can still access the non-synchronized (this) synchronization code block in a
(1.4) The Synchronized method of different object instances is non-interfering. For example, the Synchronized method in B,b, another object instance with this class, can now be accessed as usual

(2) keyword synchronized in the code snippet, similar to the Amethod () {synchronized (this) {/* block */}}, in fact, is also a lock on the instantiation of object A, this is to reduce the lock range, improve efficiency.

(3) Lock the class in the method. Similar to Amethod () {synchronized (Foo.class) {}}, this is a case of locking the whole class. It can work on all object instances of the class. At this point all instances of the class receive the lock's moderation, and only the lock can access the

(4) Declare the static member function of the class as synchronized. This lock works on all object instances of the class. Similar to 3.

Other:

(1) Synchronized keyword is not inherited, that is, the method of the base class synchronized F () {} is not automatically synchronized F () {} in the inheriting class, but instead becomes F () {}. Inheriting a class requires that you explicitly specify one of its methods as the Synchronized method.

(2) Also, bloggers about synchronized, with the room room locked metaphor, very appropriate and easy to understand.

The Synchronized keyword in Java

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.