1. What is the mechanism by which the Get method of Futuretask blocks
See its Get method source code:
/** @throws cancellationexception {@inheritDoc} */ public throws interruptedexception, executionexception { return sync.innerget (); }
It is not difficult to find that Futuretask relies on its inner class java.util.concurrent.futuretask.sync<v> class to implement blocking.
Sync also implements the Abstractqueuedsynchronizer class.
Private Final class extends Abstractqueuedsynchronizer
See who has implemented this class:
There are many of our usual use, but do not know the principle of the class, the original is to achieve abstractqueuedsynchronizer to achieve the corresponding synchronization mechanism.
What does Abstractqueuedsynchronizer do to block and maintain the coordination of resource allocation among competing threads? Look at the next paragraph.
2. Analysis Abstractqueuedsynchronizer
From the above analysis, Abstractqueuedsynchronizer should be the JDK concurrent package is a more important mechanism. Start with Google to understand his face:
Chinese blog Search results show that a lot of people have done a summary and study, the bottom box up a PDF result should be a paper on this.
English original
Chinese version
In this paper, some key points are expounded.
1. Question of method naming
“
Synchronizer generally consists of two methods, one is acquire and the other is release. The acquire operation blocks the calling thread until or unless the synchronization state allows it to continue execution. The release operation, in some way, alters the synchronization state so that one or more threads that are acquire blocked continue to execute.
The J.U.C package does not have a uniform definition of the Synchronizer's API. Therefore, there are classes that define common interfaces (such as lock), while others define their proprietary versions. So in different classes, the names and forms of the acquire and release operations are different. For example: Lock.lock,semaphore.acquire,countdownlatch.await and Futuretask.get, in this framework, these methods are acquire operations. However, J.U.C has a consistent convention between classes to support a range of common usage options. In a meaningful situation, each Synchronizer supports the following actions:
Blocking and non-blocking (for example, Trylock) synchronization.
Optional timeout setting that allows callers to abandon the wait
The cancellation of a task implemented by an interrupt is usually divided into two versions, one acquire can be canceled, and the other cannot.
”
2. Implement three "components" of the Synchronizer
“
In order to do this, the following three basic components are required to work together:
A) atomic management of the synchronization State;
(b) blocking and unblocking of threads;
c) Management of queues;
”
How to achieve these three parts, the paper is described in detail.
The paper mentions that "the key to the whole framework is how to manage the queue of blocked threads " which corresponds to part C of the three "components" above.
The CLH queue is used in Aqs.
Why use CLH instead of MCS, because CLH is easier to implement cancellation and timeout mechanisms.
3. CLH Queue
CLH Lock Concise Introduction
A
What mechanism is used by the Get method of Futuretask to block the thought of triggering