1.结构
Lock的实现类其实都是构建在AbstractQueuedSynchronizer上,每个Lock实现类都持有自己内部类Sync的实例
二。二元信号量
A semaphore initialized to one, and which is used such that it only has at most one permit available, can serve as a mutual exclusion
lock. This is more commonly known as a binary semaphore, because it only has two states: one permit available, or zero permits
available. When used in this way, the binary semaphore has the property (unlike many Lock
implementations), that the "lock" can be
released by a thread other than the owner (as semaphores have no notion of ownership). This can be useful in some specialized
contexts, such as deadlock recovery.
信号量初始化为1,有且仅有一个可用,称为互斥现象锁,常称为二元信号量,因为它仅有两个状态:一个permit 可用,或零个permit可用。
二元信号量可以实现锁被自身线程释放,而不是线程的所有者释放。因为信号量没有所有者的概念。这在死锁恢复时特别有用。
三。LockSupport
This class associates, with each thread that uses it, a permit (in the sense of the Semaphore
class). A call to park
will return immediately
if the permit is available, consuming it in the process; otherwise it may block. A call to unpark
makes the permit available, if it was not
already available. (Unlike with Semaphores though, permits do not accumulate. There is at most one.)
每个使用LockSupport的线程都和一个permit相关联, 如果permit可用,调用park()会立即返回。
park 是停车之意
dormant 休眠
spuriously 伪造地
public static void park()
Disables the current thread for thread scheduling purposes unless the permit is available.
除非permit可用,否则当前线程不能运行。
If the permit is available then it is consumed and the call returns immediately; otherwise the current thread becomes disabled for thread
scheduling purposes and lies dormant until one of three things happens:
1.Some other thread invokes unpark with the current thread as the target
2.Some other thread interrupts the current thread
3.The call spuriously (that is, for no reason) returns
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the thread
to park in the first place. Callers may also determine, for example, the interrupt status of the thread upon return.