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.

AQS 与 LockSupport的更多相关文章

  1. Java多线程系列--AQS之 LockSupport

    concurrent包是基于AQS (AbstractQueuedSynchronizer)框架的,AQS(JAVA CAS原理.unsafe.AQS)框架借助于两个类: Unsafe(提供CAS操作 ...

  2. 高效并发JUC锁-砖石

    JUC包的锁(可重入锁和读写锁) Lock是JAVA5增加的内容,在JUC(java.util.concurrent.locks)包下面,作者是并发大师Doug Lea.JUC包提供了很多封装的锁,包 ...

  3. Java 如何中断和恢复线程的执行

    一.线程的状态 线程可以阻塞于四种状态: 1.当线程执行Thread.sleep()时,它一直阻塞到指定的毫秒时间之后,或者阻塞被另一个线程打断: 2.当线程碰到一条wait()语句时,它会一直阻塞到 ...

  4. AQS2:可重入和阻塞

    本文仅基于可重入的锁(ReentrantLock类)对AQS做分析,只考虑独占锁. 共享锁与独占锁的更多信息,以后再讨论. AQS中队列的实现 节点Node AQS的节点包含了对前置节点的引用pre, ...

  5. Java并发包源码学习系列:阻塞队列BlockingQueue及实现原理分析

    目录 本篇要点 什么是阻塞队列 阻塞队列提供的方法 阻塞队列的七种实现 TransferQueue和BlockingQueue的区别 1.ArrayBlockingQueue 2.LinkedBloc ...

  6. 从软件(Java/hotspot/Linux)到硬件(硬件架构)分析互斥操作的本质

    先上结论: 一切互斥操作的依赖是 自旋锁(spin_lock),互斥量(semaphore)等其他需要队列的实现均需要自选锁保证临界区互斥访问. 而自旋锁需要xcmpchg等类似的可提供CAS操作的硬 ...

  7. 【Java并发编程实战】----- AQS(三):阻塞、唤醒:LockSupport

    在上篇博客([Java并发编程实战]----- AQS(二):获取锁.释放锁)中提到,当一个线程加入到CLH队列中时,如果不是头节点是需要判断该节点是否需要挂起:在释放锁后,需要唤醒该线程的继任节点 ...

  8. Java并发包源码学习之AQS框架(三)LockSupport和interrupt

    接着上一篇文章今天我们来介绍下LockSupport和Java中线程的中断(interrupt). 其实除了LockSupport,Java之初就有Object对象的wait和notify方法可以实现 ...

  9. AQS阻塞唤醒工具LockSupport

    LockSupport在JDK源码中描述为:构建锁和其他同步类的基本线程阻塞原语,构建更高级别的同步工具集.LockSupport提供的park/unpark从线程的粒度上进行阻塞和唤醒,park/u ...

随机推荐

  1. ZooKeeper使用原理

    ZooKeeper的基本原理 ZNode的基本概念 ZooKeeper数据模型的结构与Unix文件系统很类似,整体上可以看作是一棵树,每个节点称做一个ZNode.每个ZNode都可以通过其路径唯一标识 ...

  2. 编译包含Google Play服务App的SDK版本问题

    编译包含Google Play服务App的SDK版本问题   错误信息:No Resouce identifier found for attribute 'touchscreen BlocksFoc ...

  3. http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html

    http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html http://www.oschina.net/question/117304_51525

  4. BZOJ4068 : [Ctsc2015]app

    对于一个所选任务集合,如果对于任意时刻$i$,$i$前面所选任务数都不超过i的话,那么这些任务可以全选. 维护一棵线段树$T$,第$i$个位置一开始为$i$,每使用一个任务,$[t,T]$都要减$1$ ...

  5. windows 8 项目

    展示图  建议搜索: 线路查询: 应用商店式瀑布浏览(语义缩放): 路线规划:

  6. 套题整理 Orz DXY

    弱弱的DXY 题目描述 DXY太弱了,以至于他已经不知道要如何解决调整一个数列的使得他变成一个严格上升序列. 输入格式 第 1 行,1 个整数 N 第 2 行,N 个整数 A1,A2,...,AN 输 ...

  7. TYVJ P1002 谁拿了最多奖学金 Label:模拟 水

    背景 NOIP2005复赛提高组第一题 描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同:1)     院士奖学金,每人8000元,期末平均成绩高于80分( ...

  8. Nginx配置文件nginx.conf中文详解(总结)

     PS:这篇是目前最完整的Nginx配置参数中文说明.更详细的模块参数请参考:http://wiki.nginx.org/Main                 #定义Nginx运行的用户和用户组 ...

  9. jQuery Mobile高手必备的十大技巧和代码片段

    与任何新技术一样,常常难就难在如何开始入手. 有鉴于此,我们整理出了与jQuery Mobile库有关的我认为最便利的一些技巧.方法和代码片段. 由于本文不是旨在全面介绍使用jQuery Mobile ...

  10. Spark中加载本地(或者hdfs)文件以及SparkContext实例的textFile使用

    默认是从hdfs读取文件,也可以指定sc.textFile("路径").在路径前面加上hdfs://表示从hdfs文件系统上读 本地文件读取 sc.textFile("路 ...