Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java's <a href="http://java.sun.com/javase/7/docs/api/java/util/concurrent/locks/AbstractQueuedSynchronizer.html" rel="nofollow noreferrer"><code>AbstractQueuedSynchronizer</code></a> (and its sibling <a href="http://java.sun.com/javase/7/docs/api/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.html" rel="nofollow noreferrer"><code>AbstractQueuedLongSynchronizer</code></a>) works similarly, or at least it <em>could</em> be implemented similarly. These types form the basis for several concurrency primitives in the Java library, such as <a href="http://java.sun.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html" rel="nofollow noreferrer"><code>ReentrantLock</code></a> and <a href="http://java.sun.com/javase/7/docs/api/java/util/concurrent/FutureTask.html" rel="nofollow noreferrer"><code>FutureTask</code></a>.</p> <p>It works by way of using an atomic integer to represent state. A lock may define the value 0 as unlocked, and 1 as locked. Any thread wishing to acquire the lock attempts to change the lock state from 0 to 1 via an atomic <em>compare-and-set</em> operation; if the attempt fails, the current state is not 0, which means that the lock is owned by some other thread.</p> <p><code>AbstractQueuedSynchronizer</code> also facilitates waiting on locks and notification of <em>conditions</em> by maintaining <em>CLH queues</em>, which are lock-free linked lists representing the line of threads waiting either to acquire the lock or to receive notification via a condition. Such notification moves one or all of the threads waiting on the condition to the head of the queue of those waiting to acquire the related lock.</p> <p><em>Most</em> of this machinery can be implemented in terms of an atomic integer representing the state as well as a couple of atomic pointers for each waiting queue. The actual scheduling of which threads will contend to inspect and change the state variable (via, say, <a href="http://java.sun.com/javase/7/docs/api/java/util/concurrent/locks/AbstractQueuedSynchronizer.html#tryAcquire%28int%29" rel="nofollow noreferrer"><code>AbstractQueuedSynchronizer#tryAcquire(int)</code></a>) is outside the scope of such a library and falls to the host system's scheduler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload