[concurrent programming] explicit lock and AQS

Exclusive lock

Reentrant lock

In normal business work, reading and writing are more and less, and reading and writing are separated

Read write lock

Read thread - > read lock. Read lock is shared. Read lock is not excluded, but write lock is excluded

Write thread - > write lock, write lock exclusive, read and write mutually exclusive

Syn wait / notify

Lock Condition await/signal

Explicit lock

Lock interface and core method

Comparison between Lock interface and synchronized

The synchronized code is concise. Lock: obtaining the lock can be interrupted. Obtain the lock after timeout. Try to obtain the lock. Read more and write less and use the read-write lock

Reentrant lock ReentrantLock, so-called lock fairness and unfairness

If, in terms of time, the request to obtain the lock first must be satisfied first, the lock is fair. If it is not satisfied, it is unfair

Unfair efficiency is generally higher

ReadWriteLock interface and ReadWriteLock ReentrantReadWriteLock

ReentrantLock and Syn keywords are exclusive locks,

Read write lock: multiple read threads are allowed to access at the same time, but when the write thread accesses, all reads and writes are blocked, which is most suitable for the situation of more reads and less writes

Condition interface

Wait / notify with Lock and Condition

Learn about LockSupport tools

The method starting with park is responsible for blocking threads

unpark(Thread thread): it is responsible for waking up the thread

AbstractQueuedSynchronizer in-depth analysis

What is AQS?

AQS usage and design patterns

Inheritance, template method pattern

Methods in AQS source code

Template method:

Exclusive acquisition

accquire

acquireInterruptibly

tryAcquireNanos

Shared acquisition

acquireShared

acquireSharedInterruptibly

tryAcquireSharedNanos

Exclusive release lock

release

Shared release lock

releaseShared

Process methods requiring subclass coverage

Exclusive get {tryAcquire

Exclusive release {tryRelease

Shared access tryAcquireShared

Shared release {tryrereleaseshared

Is this synchronizer in exclusive mode

Synchronization state:

getState: get the current synchronization state

setState: sets the current synchronization state

compareAndSetState uses CAS to set the state to ensure the atomicity of the state setting

Data structure in AQS - nodes and synchronization queues

Threads that fail to compete will be packaged into nodes and placed in the synchronization queue,

Possible states of Node:

CANCELLED: the thread has timed out or been interrupted and needs to be removed from the queue

SIGNAL: the following nodes are in the waiting state. The current node notifies the following nodes to run

CONDITION: the current node is in the waiting queue

PROPAGATE: shared, indicating that the state should be propagated to the following nodes

0: indicates the initial state

Addition and removal of nodes in synchronization queue

The node joins the synchronization queue

Change of first node

Exclusive synchronous state acquisition and release

Acquisition and release of exclusive lock?

(release: traverse backward from the tail to find the actual UN cancelled subsequent items...)

Condition analysis

await/signal

    public void changeKm(){
        lock.lock();
        try {
        	this.km = 101;
        	keCond.signalAll();
        }finally {
        	lock.unlock();
        }
    }


    public  void changeSite(){
    	lock.lock();
        try {
        	this.site = "BeiJing";
        	siteCond.signal();
        }finally {
        	lock.unlock();
        }    	
    }


    public void waitKm(){
    	lock.lock();
    	try {
        	while(this.km<=100) {
        		try {
        			keCond.await();
    				System.out.println("check km thread["+Thread.currentThread().getId()
    						+"] is be notifed.");
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
        	}    		
    	}finally {
    		lock.unlock();
    	}

        System.out.println("the Km is "+this.km+",I will change db");
    }


    public void waitSite(){
    	lock.lock();
        try {
        	while(CITY.equals(this.site)) {
        		try {
        			siteCond.await();
    				System.out.println("check site thread["+Thread.currentThread().getId()
    						+"] is be notifed.");
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
        	}
        }finally {
        	lock.unlock();
        } 
        System.out.println("the site is "+this.site+",I will call user");
    }

Keywords: Java Concurrent Programming

Added by rashu.dr on Fri, 11 Feb 2022 22:07:49 +0200