CLH队列锁 及自旋锁

乐观锁及CAS

独占锁与共享锁

LockSupport与wait ,join和notify

这里截取内部类Node的部分代码,节点的状态值如下:

        /** waitStatus value to indicate thread has cancelled */
static final int CANCELLED = 1;
/** waitStatus value to indicate successor's thread needs unparking */
static final int SIGNAL = -1;
/** waitStatus value to indicate thread is waiting on condition */
static final int CONDITION = -2;
/**
* waitStatus value to indicate the next acquireShared should
* unconditionally propagate
*/
static final int PROPAGATE = -3; /**
* Status field, taking on only the values:
* SIGNAL: The successor of this node is (or will soon be)
* blocked (via park), so the current node must
* unpark its successor when it releases or
* cancels. To avoid races, acquire methods must
* first indicate they need a signal,
* then retry the atomic acquire, and then,
* on failure, block.
* CANCELLED: This node is cancelled due to timeout or interrupt.
* Nodes never leave this state. In particular,
* a thread with cancelled node never again blocks.
* CONDITION: This node is currently on a condition queue.
* It will not be used as a sync queue node
* until transferred, at which time the status
* will be set to 0. (Use of this value here has
* nothing to do with the other uses of the
* field, but simplifies mechanics.)
* PROPAGATE: A releaseShared should be propagated to other
* nodes. This is set (for head node only) in
* doReleaseShared to ensure propagation
* continues, even if other operations have
* since intervened.
* 0: None of the above
*
* The values are arranged numerically to simplify use.
* Non-negative values mean that a node doesn't need to
* signal. So, most code doesn't need to check for particular
* values, just for sign.
*
* The field is initialized to 0 for normal sync nodes, and
* CONDITION for condition nodes. It is modified using CAS
* (or when possible, unconditional volatile writes).
*/
volatile int waitStatus;

重要方法理解:

2.唤醒后继节点:

    /**
* Wakes up node's successor, if one exists.
*
* @param node the node
*/
private void unparkSuccessor(Node node) {
/*
* If status is negative (i.e., possibly needing signal) try
* to clear in anticipation of signalling. It is OK if this
* fails or if status is changed by waiting thread.
*/
int ws = node.waitStatus; //首先获取当前节点的状态值
if (ws < 0) //如果节点的状态在条件中,将当前节点变成正常节点(0 表示节点状态正常)
compareAndSetWaitStatus(node, ws, 0); /*
* Thread to unpark is held in successor, which is normally
* just the next node. But if cancelled or apparently null,
* traverse backwards from tail to find the actual
* non-cancelled successor.
*/
Node s = node.next;
if (s == null || s.waitStatus > 0) {
s = null;
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
if (s != null)
LockSupport.unpark(s.thread);
}

如方法注释,这个方法是唤醒节点的后继节点,

AQS及其前置知识总结的更多相关文章

  1. 学golang之前都需要哪些前置知识?

    我学golang,感觉前面基础语法部分都很快能学会,但是到了goroutine,channel等后面的部分就看不懂了,是不是我学这个之前还得学习其他什么知识啊?(我有C语言基础,对于C语言里面的指针, ...

  2. webpack前置知识1(模块化开发)

    webpack前置知识1(模块化开发) 新建 模板 小书匠  在开始对模块化开发进行讲解之前,我们需要有这么一个认识,即 在没有过多第三方干扰时,成本低收益高的事物更容易获得推广和信赖. 模块化开发就 ...

  3. JavaWeb前置知识 : 动态和静态的区别、两种架构、常见状态码

    JavaWeb程序设计(一) : 前置知识 1.动态网页与静态网页的区别: a.不要和是否有"动感"混为一谈. b.是否随着时间.地点.用户操作的改变而改变 (例如 : 在百度上搜 ...

  4. Java安全之Commons Collections1分析前置知识

    Java安全之Commons Collections1分析前置知识 0x00 前言 Commons Collections的利用链也被称为cc链,在学习反序列化漏洞必不可少的一个部分.Apache C ...

  5. Fastjsonfan反序列链学习前置知识

    Fastjson前置知识 Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象. Fastjson 可以操作任何 ...

  6. gitbook 入门教程之前置知识

    markdown 基本知识 markdown 是一种简化的 html 语法,相比于 txt 无格式文本更强大. 你可以用专门的软件去编辑 markdown 文件,就像需要使用软件编辑 txt 文件一样 ...

  7. WebAPI前置知识:HTTP与RestfulAPI

    对HTTP协议的基本了解是能理解并使用RestFul风格API的基础,在了解了这些基础之后,使用各种RestFul的开发框架才能得心应手.我一开始使用WebApi的时候就因为对这些知识缺乏了解,觉得用 ...

  8. C++学习2--坦克大战编写-前置知识

    基础班学习的这一个多月里的前三周讲解基础的语法,最后一周需要做坦克大战的项目巩固提高自己掌握的语法知识.这个系列博文主要是为了把学习过程中的知识点总结并记录下来: 开发语言与开发工具:C++,VS20 ...

  9. three.js基础前置知识

    这一节是纯理论知识,用于介绍three.js的入门概念,也就是开发前需要准备的理论基础. 一,三剑客 当然就是scene,camera,renderer这三个基本要素. scene是一个用于容纳三维空 ...

随机推荐

  1. YII的RBAC

    转自:http://www.cppblog.com/guojingjia2006/archive/2013/01/15/197298.html 开始准备 Yii提供了强大的配置机制和很多现成的类库.在 ...

  2. Centos下Docker安装与使用的相关命令

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2 systemctl docker status yum-config- ...

  3. 谈谈java中成员变量与成员方法继承的问题

    谈谈java中成员变量与成员方法继承的问题 关于成员变量和成员方法的的继承问题,我也可以做一个小测试,来看看结果. 首先我们先创建一个父类:

  4. Subset Sums

    链接 分析:dp[i][j]表示前i个数能够组成j的对数,可得dp[i][j]=dp[i-1][j]+dp[i-1][j-i],所以最后dp[n][sum/2]既是所求 /* PROB:subset ...

  5. html格式

    优美的代码编写方式是我们装逼的基础,在python中我们称优秀的代码为pythonic,无独有偶,html.css.js也都有着自己相比更优美的写法~ <!DOCTYPE html> &l ...

  6. java:calendar类及一些比较实用的utils(二)

    在这里将我在项目中用到的一些关于使用Calendar的utils分享出来,只是一部分,后期遇到好的通用方法会继续添加,以和大家交流学习,如果你还不熟悉这个类的使用,但是急需某个util,可以加群:41 ...

  7. Subsets Forming Perfect Squares

    题意: 给出n个数字,选出若干个数字,使得这些数字的乘积是一个完全平方数,问有多少种选法. 解法: 考虑异或方程组,$x_i$表示第i个数字是否选, 注意到只要保证结果中各个质因数都出现偶数次就可保证 ...

  8. SpringMVC配置字符过滤器的两种方式

    有时候使用SpringMVC框架提交表单时会出现中文乱码,以下是我亲自试验过的配置字符过滤器的两种: 1.在web.xml中配置 <filter> <filter-name>c ...

  9. 技术胖Flutter第三季-15垂直布局Column组件

    博客地址: https://jspang.com/post/flutter3.html#toc-8eb 垂直布局 左对齐: crossAxisAlignment: CrossAxisAlignment ...

  10. tensorflow weight_variable going

    # coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...