Java class library concurrent: learning the most core type AbstractQueuedSynchronizer

Source: Internet
Author: User

First of all, the concurrent class library should be a very important class library in java. When building some Synchronous Code, containers, and concurrency, you can find ready-made and usable classes in this class library...

The most core type in this class library is the AbstractQueuedSynchronizer type. You can implement your own synchronization tools based on it. For example, the ReentrantLock type is actually implemented based on it...

In the previous tomcat source code, its own LimitLatch is also implemented based on AbstractQueuedSynchronizer... and the code of AbstractQueuedSynchronizer is the powerful tool class Doug Lea...

Of course, you may not have the energy to thoroughly understand the spirit of the Code .. Let's take a look at how to use it ..

Well, let's talk about the use of AbstractQueuedSynchronizer...

(1) used to implement synchronization components, such as locks or something.

(2) PassGet and releaseTo implement thread blocking. At the same time, it also maintains a FIFO queue internally to maintain blocked threads ..


Here, the acquisition and release are divided into two types: one is exclusive and the other is shared. Here we should be able to understand what the two names mean...

Here we will first look at how to use the exclusive method. We will use it to implement a reentrant exclusive lock...

Public class ExLock {private class Sync extends actqueuedsynchronizer {public Sync () {super ();} protected boolean tryAcquire (int arg) {int state = this. getState (); // obtain the current state if (state = 0 & this. compareAndSetState (0, 1) {// if it is 0 and can be atomic updated to 1, it indicates that this can be locked. setExclusiveOwnerThread (Thread. currentThread (); // indicates that return true is held exclusively by this thread; // returns true, indicating that it can be locked.} if (this. getExclusiveOwnerThread () = Thread. currentThread () {// if the current thread already holds the lock, update the count. setState (state + 1); // Add 1 return true to the count; // return true, indicating that the count can be locked} return false; // return false, indicating that the lock fails, in this way, the thread will be blocked and put into the queue} protected boolean tryRelease (int arg) {if (this. getExclusiveOwnerThread () = Thread. currentThread () {// The current thread is the thread that holds the lock int state = this. getState (); // get the current count int next = state-1; // minus one, indicating the count after this release. setState (next); // update count if (next = 0) {// if it has changed to 0, it indicates that the entire lock has been released this. setExclusiveOwnerThread (null); // The hold lock is nullreturn true; // return true, indicating that the entire lock has been released, then the thread waiting for the queue will be awakened} else {return false; // It indicates that the lock is still released.} else {// only the thread holding the lock can release the lock. return false;} private Sync sync = new Sync (); public void lock () throws InterruptedException {this. sync. acquireInterruptibly (1); // tryAcquire will be called later to determine whether locks are allowed. Here Interrupt} public void unLock () {this. sync. release (1); // tryRelease will be called later to determine whether it can be released} public static void main (String args []) throws InterruptedException {ExLock lock = new ExLock (); lock. lock (); lock. unLock (); System. out. println ("aaaaa ");}}

Without a few lines of code, we can implement an exclusive lock that can be reentrant. Here, the internal class Sync works by inheriting the AbstractQueuedSynchronizer type ..

The exclusive method is as follows:

Acquire () or acquireInterruptibly (). The difference here is whether interrupt can be responded, and then the release is the release () method...

For acquire and acquireInterruptibly methods, the tryAcquire method will be called in AbstractQueuedSynchronizer to determine whether the current lock is enabled. If yes, true is returned. If no, false is returned, the current thread will be blocked and put on the internal maintenance FIFO queue... This is determined by the value of the state parameter... Then, status updates are implemented through the compareAndSetState and setState methods... These two methods must be used because they are atomic implementations...


Call the release method to release the lock. The tryRelease method is called to determine whether the lock can be released. If true is returned, the lock can be released, at the same time, it will wake up the thread waiting on the queue to continue to compete for the lock. If false is returned, it means it is not enough...


It's easy. There are no lines of code, and the function is implemented...

Of course, this is an exclusive method ..

If you want to share the lock, for example, you can obtain the lock from multiple threads, you should call the acquireShared or acquireInterruptibly method, and call the releaseShared method when releasing the lock...

Similarly, the methods to be implemented should be tryAcquireShared and tryReleaseShared ..

Well, I won't go into detail here. I just need to know how to use it...

If you are interested, you can read the implementation of those locks in the concurrent library...


Related Article

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.