Java synchronized keyword usage and level of Lock: Method Lock, Object lock, Class lock

Source: Internet
Author: User
Tags instance method
Java synchronized keyword usage and level of Lock: Method Lock, Object lock, Class lock

Reproduced from: http://blog.csdn.net/le_le_name/article/details/52348314

Java built-in locks: Each Java object can be used as a lock that implements synchronization, and these locks become built-in locks. The lock is automatically acquired when a thread enters a synchronized code block or method, which is released when the code block or method is exited. The only way to get a built-in lock is to enter the synchronized code block or method of the lock's protection.

A Java built-in lock is a mutex, this means that at most only one thread can acquire the lock, and when thread a attempts to obtain the built-in lock held by thread B, thread A must wait or block, knowing that thread B frees the lock and that a thread will wait forever if the B thread does not release the lock.

Java object locks and class Locks: Java object Locks and class locks are essentially the same as built-in locks in the concept of locks, however, the two locks are actually very different, object locks are used for object instance methods, or on an object instance, class locks are used for static methods of classes or for class objects of classes. We know that the object instance of a class can have many, but each class has only one class object, so object locks on different object instances are not disturbed, but each class has only one class lock. But one thing to be aware of is that the class lock is just a conceptual thing, not real, it's just used to help us understand the difference between a locked instance method and a static method.

The above has a little understanding of some of the concepts of the lock, the following discusses the use of the Synchronized keyword.

Synchronized usage: Synchronized cosmetic method and synchronized cosmetic code block.


Actually, class lock modification methods and code block effects and object locks are the same, because class locks are only an abstract concept, only to distinguish the characteristics of static methods, because the static method is common to all object instances, so it corresponds to the synchronized modified static method of the lock is unique, So abstract out a class lock. In fact here's the emphasis here in this block of code, synchronized simultaneously modifies both static and Non-static methods



Synchronized flaw: When a thread enters the synchronization method to acquire an object lock, other threads must wait or block when accessing the synchronization method of the object here, which is fatal to the highly concurrent system, which can easily cause the system to crash. If a thread has a dead loop in the synchronization method, it will never release the object lock, then the other threads will wait forever. This is a fatal problem.


Object locks of one class are not associated with object locks of another class, and when a thread obtains an object lock of Class A, it can also acquire object locks of Class B.


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.