Java concurrency-exclusive and shared locks

Source: Internet
Author: User
Tags semaphore

1 monopoly and sharing of locks

The lock mode provided by Java and the package is divided into exclusive and shared locks, in exclusive lock mode, only one thread can hold the lock at a time, Reentrantlock is the exclusive mutex. Shared locks allow multiple threads to acquire locks concurrently and concurrently access shared resources, such as: Readwritelock. AQS's internal class node defines two constants, shared and exclusive, that identify the lock acquisition mode of the waiting thread in the Aqs queue, respectively.

Obviously, an exclusive lock is a pessimistic and conservative locking policy that avoids read/read collisions, and if a read-only thread acquires a lock, other read threads can wait only, which in this case restricts unnecessary concurrency because the read operation does not affect the consistency of the data. A shared lock is an optimistic lock that relaxes the lock policy and allows multiple threads that perform read operations to access shared resources at the same time. Readwritelock, read-write locks are available in Java's concurrency package. It allows a resource to be accessed by multiple read operations, or accessed by a write operation, but not both.

2 fair and non-fair of Locks

The fairness and unfairness of the lock is whether the thread is allowed to queue in the process of acquiring the lock. In a fair lock, the thread acquires the lock in the order in which they make the request, while the non-fair lock allows the threads to attempt to acquire the lock immediately after the request is made and, if available, to obtain the lock directly and attempt to fail before waiting. Reentrantlock provides two methods of lock acquisition, Fairsyn and Nofairsync. Conclusion: Reentrantlock is a mutex which is implemented by the locking policy of exclusive lock, and it provides both fair and non-fair lock acquisition methods. When we first looked at the source code, they confused the two concepts.

3 template methods provided by Aqs

AQS provides a way for exclusive and shared locks to be implemented, subclasses with exclusive lock functionality, which must implement Tryacquire, Tryrelease, isheldexclusively, and so on, subclasses of the shared lock function, Methods such as tryacquireshared and tryreleaseshared must be implemented, and the method with shared suffixes is to support the semantics of shared lock and lock. Semaphore is a shared lock, and Reentrantlock is an exclusive lock.

When an exclusive lock acquires a lock, the Set node mode is Node.exclusive

Public final void acquire (int arg) {        if (!tryacquire (ARG) &&            acquirequeued (Addwaiter (node.exclusive), ARG))            selfinterrupt ();    }

Shared lock acquisition Lock, node mode is node.shared

     private void doacquireshared (int arg) {        final node node = addwaiter (node.shared);        Boolean failed = true;        .....    }

4 understanding of the Conditionobject

The Reentrantlock is an exclusive lock, and the Aqs Conditionobject can only be used with Reentrantlock, which is more convenient to support the lock on the conditional queue. Conditionobject's signal and await methods are all based on exclusive locks, and if the line Cheng Fei the exclusive thread of the lock, the illegalmonitorstateexception is thrown. For example Signalall Source:

        Public final void Signalall () {            if (!isheldexclusively ())                throw new Illegalmonitorstateexception ();            Node first = Firstwaiter;            if (first! = null)                Dosignalall (first);        }

I was thinking, since condtion is to support lock, why conditionobject not as the inner class of Reentrantlock? For subclasses that implement the lock function, it is possible to extend it directly to support the conditional queue. However, for other non-lock semantic implementation classes such as semaphore, Countdownlatch and other classes, the conditional queue is useless, but also to developers to expand AQS confusion. In short, there are pros and cons, the ideas of the Masters, but also need to carefully explore AH!

Java concurrency-exclusive and shared 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: 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.