preface

To recap:

  • Multiple threads can get through a door in three minutes!
  • Thread source code analysis
  • Multithreading foundation essential knowledge points! Read the learning multithreading twice the result with half the effort
  • Learn about the Java locking mechanism
  • AQS goes over it briefly

Only skinheads can be strong!

The basic AQS of Lock locks have been briefly covered in the previous article, so this article will mainly explain the two main subclasses of Lock locks:

  • ReentrantLock
  • ReentrantReadWriteLock

So let’s get started

A, already locked

First let’s look at the comments at the top of the ReentrantLock lock to see its related features:

To summarize the main points:

  • More flexible than synchronized
  • Support fair locks (relatively fair)
  • The most standard use is to call the lock method before a try and release the lock in the finally block

class X {
    private final ReentrantLock lock = new ReentrantLock();
    // ...

    public void m(a) { 
        lock.lock();  // block until condition holds
        try {
            // ... method body
        } finally {
            lock.unlock()
        }
    }
}
Copy the code

1.1 the inner class

First we can see that there are three inner classes:

These inner classes are all subclasses of AQS, which is the basis for ReentrantLock, and AQS is the framework for building locks and synchronizers

  • You can clearly see that our ReentrantLock lock supports both fair and unfair locks

1.2 Construction method

1.3 Unfair Lock method

Attempt to acquire the lock, if it fails, call AQS acquire(1)

Acquire(1) method we saw briefly in AQS, where tryAcquire() is implemented by subclasses

Let’s go to tryAcquire() :

1.4 Fair Lock method

The fair lock method adds a state condition:

This method mainly determines whether the current thread is the first in the CLH synchronization queue. Flase is returned if it is, true otherwise.

1.5 unlock method

The unlock method is also defined in AQS:

To see how tryRelease(ARG) is implemented:

Second, the ReentrantReadWriteLock

We know that synchronized built-in locks and reentrantLocks are mutually exclusive (only one thread can enter a critical area at a time).

ReentrantReadWriteLock is a read/write lock:

  • When reading data, multiple threads can enter the critical area (locked area) simultaneously.
  • When writing data, both the reader and writer threads are mutually exclusive

As a general rule: most of us read more data and modify less. So this read-write lock is useful in this scenario!

Read/write locks have one interface, ReadWriteLock, which defines two methods:

Let’s see what the comment at the top says:

In fact, probably also explained: reading can be shared, while writing is mutually exclusive

Let’s look at the corresponding implementation class:

Take a quick look at the top comment as usual:

So we can summarize some key points of read/write locking:

  • Read locks do not support conditional objects. Write locks support conditional objects
  • A read lock cannot be upgraded to a write lock, and a write lock can be downgraded to a read lock
  • Read/write locks also have fair and unfair modes
  • Read locks allow multiple read threads to enter the critical area. Write locks are mutually exclusive

2.1 ReentrantReadWriteLock inner classes

ReentrantReadWriteLock has two more internal classes (both Lock implementations) than ReentrantLock to maintain read and write locks, but the body still uses Syn:

  • WriteLock
  • ReadLock

2.2 Status representation of read lock and write lock

On a ReentrantLock lock, state is used to indicate synchronization status (which can also indicate the number of reentrants), while on ReentrantReadWriteLock, state is used to indicate read and write status:

2.3 Obtaining write Locks

Acquire (1) :

Go in and see the implementation:

2.4 Obtaining read locks

AcquireShared (int arg)

DoAcquireShared (arG); Method (also implemented in Syn), let’s look at:

Three, the last

Here’s a quick summary of this article:

  • AQS is the basis for ReentrantReadWriteLock and ReentrantLock because the default implementation is in the internal Syn class, which inherits from AQS
  • Both ReentrantReadWriteLock and ReentrantLock support fair and unfair mode. Fair mode will check whether the FIFO queue thread is in the queue head, but not fair mode
  • ReentrantReadWriteLock is a read/write lock that can be used if there are many more read threads than write threads. It uses the state variable 16 bits higher for a read lock and 16 bits lower for a write lock
  • A write lock can be downgraded to a read lock, but a read lock cannot be upgraded to a write lock
  • Write locks are mutually exclusive, while read locks are shared.

In general, look at multi-threaded source code difficulty coefficient or good high ah, my current level can only pass the….

There are many advanced knowledge points behind multithreading: Future, synchronous container ah, blocking queue, various atomic classes ah, etc., here I intend to put on the first, the current level is limited ~~~~~

There will probably be a thread pool blog post, so stay tuned

If you are interested, go to the resources below

References:

  • Cmsblogs.com/?page_id=11…

If the article has the wrong place welcome to correct, everybody exchanges with each other. Students who are used to reading technical articles on wechat and want to get more Java resources can follow the wechat public account :Java3y.

Article table of Contents navigation:

  • Zhongfucheng.bitcron.com/post/shou-j…