We know that a thread will be blocked and joined in the wait queue after attempting to acquire a lock, what kind of queue is it? And how do you manage this queue? This section chats the CHL Node FIFO queue.
Before we talk about the CHL Node FIFO queue, we analyze several elements of this queue first. The first thing to know is the spin lock, which is a thread that is trying to acquire a lock, and if the lock is already occupied by another thread, it will constantly cycle through the lock to see if it is released, rather than having this thread hang or sleep. It is a locking mechanism proposed to ensure shared resources, similar to the mutex, which guarantees that the public resources can be used by only one thread at any time, except that the mutex will go to sleep or block state after acquiring the lock failure. 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 look at the bold red two methods lock and Unlock,unsafe only for the operation to provide a hardware-level atomic CAS operation, temporarily ignore this class, as long as it knows its role can be, we will in the later "How to guarantee the Atomicity" More in-depth elaboration of this in the section. For the lock method, if there are several threads competing, the thread that can successfully modify the value to NEWV through the CAS operation is the thread that successfully acquires the lock, and the other thread constantly checks to see if the value is changed back to 0 in the loop. Instead of changing the value to 0, the lock is freed after the thread that acquires the lock is executed, the lock is freed by the Unlock method, and several threads are competing for the lock after the release. As a result, locks that are not acquired are not suspended or blocked, but are continuously checked for status. Figure 2-5-9-3 can deepen the understanding of the spin lock, five threads poll the value variable, T1 gets successful after the value is set to 1, this state when other threads are unable to compete for the lock, T1 uses the lock to set the value to 0, the remaining threads continue to compete for the lock, and so on. This guarantees thread safety for a block of a zone.
Figure 2-5-9-3 Spin Lock
The spin lock is suitable for short lock taking time, that is, the lock protection critical area is very small situation, and it needs hardware level operation, but also to ensure the consistency of the cache data, in addition, can not guarantee fairness, not guaranteed first-come first obtained, may cause thread starvation. On multiprocessor machines, each thread corresponds to a processor that reads and writes the same variable, and each read and write operation will synchronize each processor cache, causing a serious degradation in system performance.
Java concurrency framework--aqs blocking Queue management (i)