- 浏览: 1238313 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (193)
- ant/maven (6)
- algorithm (5)
- tomcat/weblogic/jboss (6)
- javascript/jquery (13)
- java (33)
- flex/flash (0)
- JPA/Hibernate/myBatis (18)
- java concurrent (7)
- test (2)
- windows/linux (6)
- java collection (7)
- design pattern (2)
- life/health (3)
- database (12)
- IDE (4)
- spring/ejb (20)
- html/css/ckeditor (7)
- jsp/servlet (3)
- java io (13)
- java security (4)
- jni (0)
- svn/git (2)
- english (2)
- java jmx (1)
- xml (1)
- struts/springmvc (9)
- middleware (2)
- cache (1)
- cglib (3)
最新评论
-
jlotusYo:
博主,真感谢。
Java 密码扩展无限制权限策略文件 -
senninha:
这个。。是api说明吧。。
ScheduledExecutorService 源码分析 -
zoutao2008:
请问大文件如何处理?按你这种方式的话,文件超过200M时就会报 ...
hessian系列之二:上传文件 -
lwj1113:
lwj1113 写道谢谢博主这么细致的demo;在系列五中通过 ...
myBatis系列之五:与Spring3集成 -
lwj1113:
谢谢博主这么细致的demo;在系列五中通过testng测试类跑 ...
myBatis系列之五:与Spring3集成
维基百科:
In computer science, the compare-and-swap CPU instruction ("CAS") (or the Compare & Exchange - CMPXCHG instruction in the x86 and Itanium architectures) is a special instruction that atomically (regarding intel x86, lock prefix should be there to make it really atomic) compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value. This guarantees that the new value is calculated based on up-to-date information; if the value had been updated by another thread in the meantime, the write would fail. The result of the operation must indicate whether it performed the substitution; this can be done either with a simple Boolean response (this variant is often called compare-and-set), or by returning the value read from the memory location (not the value written to it). Compare-and-Swap (and Compare-and-Swap-Double) has been an integral part of the IBM 370(and all successor) architectures since 1970. The operating systems which run on these architectures make extensive use of Compare-and-Swap (and Compare-and-Swap-Double) to facilitate process (i.e., system and user tasks) and processor (i.e., central processors) parallelism while eliminating, to the greatest degree possible, the "disabled spin locks" which were employed in earlier IBM operating systems. In these operating systems, new units of work may be instantiated "globally", into the Global Service Priority List, or "locally", into the Local Service Priority List, by the execution of a single Compare-and-Swap instruction. This dramatically improved the responsiveness of these operating systems.
===================================================
总结:CAS是硬件CPU提供的元语,它的原理:我认为位置 V 应该包含值 A;如果包含该值,则将 B 放到这个位置;否则,不要更改该位置,只告诉我这个位置现在的值即可。
Java并发库中的AtomicXXX类均是基于这个元语的实现,以AtomicInteger为例:
其中,unsafe.compareAndSwapInt()是一个native方法,正是调用CAS元语完成该操作。
===================================================
顺便说下volatile。
根据Java Language Specification中的说明, jvm系统中存在一个主内存(Main Memory或Java Heap Memory),Java中所有变量都储存在主存中,对于所有线程都是共享的。
每条线程都有自己的工作内存(Working Memory),工作内存中保存的是主存中某些变量的拷贝,线程对所有变量的操作都是在工作内存中进行,线程之间无法相互直接访问,变量传递均需要通过主存完成。
所以,同一变量的值在工作内存和主存中可能不一致。volatile其实是告诉处理器, 不要将我放入工作内存, 请直接在主存操作我。
===================================================
回到AtomicInteger,注意到value的声明:
通过前面对volatile的介绍可以知道,直接对volatile的赋值和读取操作是无须加锁的,见源码:
注意:volatile的这种作用对double和long无效,因为JVM中将对double或long的赋值或读取操作拆成2个32位数的操作。
===================================================
那么,为什么自增操作要通过CAS来完成呢?仔细观察incrementAndGet()方法,发现自增操作其实拆成了两步完成的:
由于valatile只能保证读取或写入的是最新值,那么可能出现以下情况:
1 A线程执行get()操作,获取current值(假设为1)
2 B线程执行get()操作,获取current值(为1)
3 B线程执行next = current + 1操作,next = 2
4 A线程执行next = current + 1操作,next = 2
这样的结果明显不是我们想要的,所以,自增操作必须采用CAS来完成。
In computer science, the compare-and-swap CPU instruction ("CAS") (or the Compare & Exchange - CMPXCHG instruction in the x86 and Itanium architectures) is a special instruction that atomically (regarding intel x86, lock prefix should be there to make it really atomic) compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value. This guarantees that the new value is calculated based on up-to-date information; if the value had been updated by another thread in the meantime, the write would fail. The result of the operation must indicate whether it performed the substitution; this can be done either with a simple Boolean response (this variant is often called compare-and-set), or by returning the value read from the memory location (not the value written to it). Compare-and-Swap (and Compare-and-Swap-Double) has been an integral part of the IBM 370(and all successor) architectures since 1970. The operating systems which run on these architectures make extensive use of Compare-and-Swap (and Compare-and-Swap-Double) to facilitate process (i.e., system and user tasks) and processor (i.e., central processors) parallelism while eliminating, to the greatest degree possible, the "disabled spin locks" which were employed in earlier IBM operating systems. In these operating systems, new units of work may be instantiated "globally", into the Global Service Priority List, or "locally", into the Local Service Priority List, by the execution of a single Compare-and-Swap instruction. This dramatically improved the responsiveness of these operating systems.
===================================================
总结:CAS是硬件CPU提供的元语,它的原理:我认为位置 V 应该包含值 A;如果包含该值,则将 B 放到这个位置;否则,不要更改该位置,只告诉我这个位置现在的值即可。
Java并发库中的AtomicXXX类均是基于这个元语的实现,以AtomicInteger为例:
public final int incrementAndGet() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return next; } } public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); }
其中,unsafe.compareAndSwapInt()是一个native方法,正是调用CAS元语完成该操作。
===================================================
顺便说下volatile。
根据Java Language Specification中的说明, jvm系统中存在一个主内存(Main Memory或Java Heap Memory),Java中所有变量都储存在主存中,对于所有线程都是共享的。
每条线程都有自己的工作内存(Working Memory),工作内存中保存的是主存中某些变量的拷贝,线程对所有变量的操作都是在工作内存中进行,线程之间无法相互直接访问,变量传递均需要通过主存完成。
所以,同一变量的值在工作内存和主存中可能不一致。volatile其实是告诉处理器, 不要将我放入工作内存, 请直接在主存操作我。
===================================================
回到AtomicInteger,注意到value的声明:
private volatile int value;
通过前面对volatile的介绍可以知道,直接对volatile的赋值和读取操作是无须加锁的,见源码:
public final int get() { return value; } public final void set(int newValue) { value = newValue; }
注意:volatile的这种作用对double和long无效,因为JVM中将对double或long的赋值或读取操作拆成2个32位数的操作。
===================================================
那么,为什么自增操作要通过CAS来完成呢?仔细观察incrementAndGet()方法,发现自增操作其实拆成了两步完成的:
int current = get(); int next = current + 1;
由于valatile只能保证读取或写入的是最新值,那么可能出现以下情况:
1 A线程执行get()操作,获取current值(假设为1)
2 B线程执行get()操作,获取current值(为1)
3 B线程执行next = current + 1操作,next = 2
4 A线程执行next = current + 1操作,next = 2
这样的结果明显不是我们想要的,所以,自增操作必须采用CAS来完成。
发表评论
-
Netty系列之二、Netty组件
2014-10-12 19:55 0Netty主要由以下几个组件构成: Bootstrap o ... -
Netty系列之二:传输方式
2014-09-17 22:35 0NIO (Non-blocking I/O) io.netty ... -
Java正则表达式实例
2014-08-25 22:50 1896题目: 有两个文件context.txt和words ... -
quartz系列之九:存储
2014-08-05 13:16 0这里以1.8.x为例: 任务 ... -
JVM 四种引用
2014-04-24 19:56 17241. 强引用 指通过普通 ... -
Proxy 源码分析
2014-04-21 10:47 0public class Proxy implements ... -
NIO UDP 编程
2014-04-17 23:18 4212和TCP的SocketChannel类似,UDP的Dat ... -
Matcher 源码分析
2014-04-15 14:45 0首先看下接口: public interface Matc ... -
BTrace 使用
2014-03-20 18:03 0简介 Btrace (Byte Trace)是Sun推出的一款 ... -
NIO Socket 编程
2014-04-11 22:48 1612Java NIO (Nonblocking IO)解决了常规I ... -
hessian系列之二:上传文件
2014-02-17 17:16 6158hessian较早版本通过 byte[] 进行文件传输;4.0 ... -
hessian系列之三:与Spring集成
2014-02-17 20:21 17817Spring封装了hessian客户端和服务端的通用代码,把实 ... -
hessian系列之一:Hello world
2014-01-06 20:51 2261Hessian是一个Web Service的轻量级二进制协议, ... -
XStream:自定义转换器
2013-12-30 22:47 0XStream是一款不错的oxm (Object-XML ma ... -
Http连接工具类
2013-12-28 16:13 0public class HttpConnUtil { ... -
Integer源码分析
2013-12-26 19:59 0private static String toU ... -
全排序
2013-12-23 21:02 0写一个函数, 如 foo(String str), 打印出 s ... -
logback系列之四:输出日志到不同文件
2013-12-03 16:25 68769logback系列之一:输出日志到控制台 logback系列之 ... -
Properties 源码分析
2013-11-26 10:32 01. Properties类扩展了Hashtable,用来保存 ... -
logback系列之三:输出日志到文件(滚动)
2013-11-16 23:37 64286logback系列之一:输出日志到控制台 logback系列之 ...
相关推荐
JAVA CAS原理深度分析,JAVA CAS原理深度分析,JAVA CAS原理深度分析,JAVA CAS原理深度分析,JAVA CAS原理深度分析,JAVA CAS原理深度分析
Java CAS原理深度分析
Java CAS 原理分析整理...Java CAS 原理分析整理...Java CAS 原理分析整理...Java CAS 原理分析整理...Java CAS 原理分析整理...Java CAS 原理分析整理...
cas java 实例 已经经过测试的实例,比较好用,可以尽快下载使用以下
3、这是一个工具类调用非常方便,不光适用于java也适用于android, 其他语言也可以参考,注释写的非常清楚详细。 调用代码如下所示: boolean flag = validate("admin","123456",...
CAS单点登录(java)CAS单点登录(java)
Yale CAS SSO JAVA Client
java cas server 集成 java cas client 和 net client 附带文档,问题解决方法,源码,jar包,包含技术有java cas,cfx,LDAP,net cas。核心在于集成了net client,,上传太小,不能传源码,有需要的加群。有兴趣的...
单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决...CAS Client 支持非常多的客户端(这里指单点登录系统中的各个 Web 应用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等。
CAS源码分析图,
JAVA开发专家:敏捷Acegi、CAS:构建安全的Java系统 pdf
基于Java中CAS的单点登录,有服务端的所有源码,将tomcat目录下的所有资源直接拷到Tomcat服务中间件的webapp目录下,阅读tomcat-webapp中的read.txt文档,查看使用说明,适用于第一次开发CAS单点登录的同学们,简单...
java cas单点登录,里面附带源码和文档,比较详细,如有不懂请加群87115896
敏捷Acegi、CAS++构建安全的Java系统pdf——part5
用于CAS配置应用程序端,将lib下jar文件放到需要整合的应用lib下
CAS Server 是一套基于 Java 实现的服务,该服务以一个 Java Web Application 单独部署在与 servlet2.3 兼容的 Web 服务器上,另外,由于 Client 与 CAS Server 之间的交互采用 Https 协议,因此部署 CAS Server 的...
sso/cas单点登录Java maven版 含服务端客服端
优化SSO登录,提速N倍,支持java、PHP、.NET等集成,创新发明,性能优异。
CAS协议分析