Java Concurrency --- display lock and AQS (a)

Foreword

Concurrency in Java, there are two ways, one is locked, the other is an atomic operation of CAS. While in the lock is divided into two types, one is the built form of the Java language level (that is, source code is not related to the class to implement) ---- Synchronized keyword, the other one is the Java source code is related implementation classes, such as Lock interface. Today, we are focused on is the description of the latter.

Lock the interface and synchronized comparison:

1) synchronized: When implementing concurrent code is relatively simple and
2) Lock: acquiring the lock can be interrupted to acquire the lock timeout can be achieved, attempts to obtain the lock mechanisms. (Case of reading and writing a small read-write lock may also be used)
3) There are two important classes of interfaces and a lower Lock Interface: ReentrantLock (reentrant lock), Condition, ReadWriteLock (write lock)

1.ReentrantLock
(1) ReentrantLock (reentrant lock) can get the equivalent of several times the same lock - "(at this level to understand the class to achieve synchronized under the Lock)
(2) reentrant lock ReentrantLock, the so-called fair and unfair lock
request on time if, firstly locks acquired must first be met, this lock is fair. If not, this kind of lock is unfair. (Nonfair allow interruption)
(. 3) of ReentrantLock (Boolean fair) in the reentrant lock constructor wherein the parameter value is used to indicate a fair fair and unfair latch lock, the default non-fair.
In general, non-fair lock of relatively high efficiency. (? Why)
The following give a simple example: process thread A and thread B runs, the thread A first obtain a lock, then thread B in a suspended state, hang in thread B stage, and the thread is created in C active state, then the thread a releases the lock, the lock can be obtained directly thread C, C Assuming thread releases the lock, the thread B from just ready to hang, and released the lock, then the efficiency of relatively fair locks to improve .
(4) ReentrantLock and synchronized keywords are exclusive lock (the same time allowing only one thread access)

2.ReadWriteLock the interface and read-write locks ReentrantReadWriteLock:
(1) read-write lock: the same time, allows you to run multiple threads simultaneously read access, but write access when all the threads read and write threads are blocked, ideal for reading and more under less write circumstances.
Based on the above mechanism, ReentrantReadWriteLock read and write locks relegation:
allow write lock downgraded to a read lock, but not read locks upgraded to a write lock.
The reason may be downgraded: Read lock is blocking write lock, locks are released only after reading and writing in order to obtain the lock. Then the thread can be used to see things written.
The reason not to upgrade: When there are threads access to read and write locks, read locks get the thread may not see the contents of the write lock written.
(2) ReentrantReadWriteLock understood achieved:
the reentrant lock except that there are two different read-write lock state, that read and write locks, (only need to set a state in the state reentrant lock lock can be controlled ) then the read-write lock is how locks of different performance status of it?
a. Set the read-write state is divided by a cut 32 in half-byte, 16-bit high-read state to save. The lower 16 bits write state to save.
B. Each thread reentrant read at this time is a local variable to store the number of threads ThreadLocalHoldCounter each of the locking reentrant.

(It will be appreciated waiting notification mode in synchronized) 3.Condition Interface
(1) and Condition implementation Lock waiting notification (wherein the Condition interface under the await () method is similar to wait at the Synchronized Object Method ())
notification method : singal () similar to the notify () singalAll () similar notifyAll ()
(2) may be a new Lock a plurality Condition

4. Understand LockSupport tools:
(1) action: blocking a thread and wake up a thread
(2) method at the beginning of the park represents a blocking
unpark (Thread thread): way to represent Wake

Published 19 original articles · won praise 2 · Views 417

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/103881116