Java concurrency framework-AQS blocking Queue Management (1), javaaqs

Source: Internet
Author: User

Java concurrency framework-AQS blocking Queue Management (1), javaaqs
We know that a thread will be blocked and added to the waiting queue After failed attempts to obtain the lock. What kind of queue is it? How to manage this queue? In this section, we will talk about the chlorophyll Node FIFO queue.
Before talking about the chlorophyll Node FIFO queue, we should analyze several elements of this queue. The first thing we need to know is the spin lock. The so-called spin lock means that when a thread tries to obtain a lock, if the lock has been occupied by other threads, this thread will repeatedly check whether the lock is released, rather than suspending or sleep the thread. It is a lock mechanism proposed to ensure resource sharing. Similar to mutex lock, it ensures that public resources can be obtained and used by up to one thread at any time, the difference is that the mutex lock will enter sleep or blocked status after the lock fails to be obtained. The following code is used to implement a simple spin lock,
Public class SpinLock {
Private static Unsafe unsafe = null;
Private static final long valueOffset;
Private volatile int value = 0;
Static {
Try {
Unsafe = getUnsafeInstance ();
ValueOffset = unsafe. objectFieldOffset (SpinLock. class
. GetDeclaredField ("value "));
} Catch (Exception ex ){
Throw new Error (ex );
}
}
Private static Unsafe getUnsafeInstance () throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
Field theUnsafeInstance = Unsafe. class. getDeclaredField ("theUnsafe ");
TheUnsafeInstance. setAccessible (true );
Return (Unsafe) theUnsafeInstance. get (Unsafe. class );
}
Public void lock (){
For (;;){
Int newV = value + 1;
If (unsafe. compareAndSwapInt (this, valueOffset, 0, newV )){
Return;
}
}
}
Public void unlock (){
Unsafe. compareAndSwapInt (this, valueOffset, 1, 0 );
}
}
This is a very simple spin lock, mainly because of the bold and Red Two Methods of lock and unlock, Unsafe is only to provide hardware-level atomic CAS operations for the operation, temporarily ignore this class, as long as you know its role, we will further elaborate on it in the "how to ensure atomicity" section below. For the lock method, if there are several threads competing, the thread that successfully modifies the value to newV through the CAS operation is the thread that successfully acquires the lock and will directly pass, other threads continuously check whether the value is changed back to 0, and the operation to change the value to 0 is to release the lock after the thread that obtains the lock is executed, the unlock method is used to release the lock. After the lock is released, several threads compete for the lock. In this way, unacquired locks will not be suspended or blocked, but will continuously check the status cyclically. Figure 2-5-9-3 can deepen the understanding of the spin lock. Five threads Round-Robin the value variable. After t1 is obtained successfully, the value is set to 1. In this status, other threads cannot compete for the lock, t1 sets the value to 0 after the lock is used, and the remaining threads continue to compete for the lock, and so on. This ensures the thread security of blocks in a region.
 
Figure 2-5-9-3 spin lock
The spin lock is suitable for scenarios where the lock takes a short time, that is, when the lock protection critical section is very small. It also requires hardware-level operations and ensures the consistency of cache data. In addition, it cannot guarantee the fairness, it is not guaranteed to be obtained first, which may cause thread hunger. On a multi-processor machine, the processor corresponding to each thread reads and writes the same variable, and each read/write operation will synchronize the cache of each processor, resulting in a serious reduction in system performance.

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.