Synchronized and Lock Object locks, mutex locks, shared locks, and fair locks and unfair locks

Source: Internet
Author: User

Synchronized and lock are used to implement thread synchronization locks. Synchronized Object locks are an interface. Their implementations include reentrantlock mutex locks and reentrantreadwritelock shared locks.

Here is a description of the reentrantreadwritelock shared lock. The so-called share is that the lock provides read/read lock sharing, that is, multiple threads can share one read lock, but the read/write locks and read/write locks are mutually exclusive.


There are many types of locks introduced on the Internet, including Object locks, mutex locks, shared locks, and fair locks and non-fair locks, but the descriptions are not detailed enough, here, we will record it in straightforward words for reference,


Object lock: It generally refers to the synchronized lock, which is related to the object and each object will have an invisible monitor for thread synchronization, methods for interacting with this object thread generally include wait, policy, and policyall,

Call the wait method. The thread enters the object's monitor and waits for other threads to call y or yyall. It should be noted that wait and notify or notifyall cannot precisely wake up a specific thread. The usage of this lock, please refer to this article.

Mutex lock: literally, mutual exclusion, that is, the two threads obtain the lock is mutually exclusive. One thread obtains the lock, and other threads cannot obtain the lock before releasing the lock, java provides a mutex lock reentrantlock, which can be reentrant

Re-entry means that after a thread obtains the lock, the thread can enter the Lock of this object in any other method. We know that synchronized interacts with threads through wait and notify, so reentrantlock is implemented through

What implements Interaction? Here we introduce an important APICondition,What is condition,ConditionSetObjectMonitor method (Wait,NotifyAndPolicyall,

In orderLockTo provide multiple waiting sets (wait-set) for each object ). Where,LockReplacedSynchronizedUse of methods and statements,ConditionReplaces object monitoring

Method. This is the explanation of condition on the official website. It can be seen that the functions provided by condition are the same as those provided by wait and condition y, but condition is used in lock, while wait and condition y are used together with synchronized.

Here, it should be noted that the wait and wake-up mechanism provided by condition clearly specifies which thread of wait and which thread to wake up. For the usage of reentrantlock and condition, please refer to this article.

Shared lock: As mentioned earlier, multiple threads can share locks. Here, the lock implementation class reentrantreadwritelock and reentrantreadwritelock are divided into read locks and write locks. the greatness of the lock here is, provides read/write lock Separation

The advantage of separation is that read locks can be shared, that is, multiple threads can share read locks, read/write mutex, and read/write mutex are the main features of this lock. The following is the source code structure of the implementation of this class:

There is an AQS. This isAbstractqueuedsynchronizerClass abbreviation. This class is the focus of lock implementation. If you are interested, you can study the source code. Sync is the implementation of AQS. In reentrantlock and reentrantreadwritelock

Classes, and fairsync and nonfairsync are so-called fair locks and unfair locks.

Synchronized and Lock Object locks, mutex locks, shared locks, and fair locks and unfair locks

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: 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.