CAS

http://wangyuanzju.blog.163.com/blog/static/130292008031101953399/

http://en.wikipedia.org/wiki/Compare-and-swap

 

java 中应用到的CAS:

http://my.oschina.net/u/177808/blog/166819

http://hi.baidu.com/heelenyc/item/b7caf283b8610fceb17154e7

http://blog.csdn.net/hsuxu/article/details/9467651


Lock and Synchronized from Java doc:

Lock implementations provide more extensive locking operations than can be obtained using synchronized methods and statements. They allowmore flexible structuring, may have quite different properties, and maysupport multiple associated Condition objects.


...


The use of synchronized methods or statements provides access to the implicit monitor lock associated with every object, butforces all lock acquisition and release to occur in a block-structured way: whenmultiple locks are acquired theymust be released in the opposite order, and all locks must be released in the same lexical scope in which they were acquired.


While the scoping mechanism for synchronized methods and statementsmakes it much easier to program with monitor locks, and helpsavoid many common programming errors involving locks, there are occasions where you need to work with locks in a more flexible way. For example, **some algorithms* for traversing concurrently accessed data structuresrequire the use of "hand-over-hand" or "chain locking": you acquire the lock of node A, then node B, then release A and acquire C, then release B and acquire D and so on. Implementations of theLock interface enable the use of such techniques by allowing a lock to be acquired and released in different scopes, andallowing multiple locks to be acquired and released in any order.


With this increased flexibility comes additional responsibility. Theabsence of block-structured locking removes the automatic release of locks that occurs with synchronized methods and statements. In most cases, the following idiom should be used:


...


When locking and unlocking occur in different scopes, care must be taken toensure that all code that is executed while the lock is heldis protected by try-finally or try-catch to ensure that the lock is released when necessary.


Lock implementations provide additional functionality over the use of synchronized methods and statements by providing anon-blocking attempt to acquire a lock (tryLock()), an attempt toacquire the lock that can be interrupted (lockInterruptibly(), and an attempt toacquire the lock that can timeout (tryLock(long, TimeUnit)).


区别并不是CAS,两者底层是CAS:

区别:前者乐观锁,后者是悲观锁实现

http://developer.51cto.com/art/201111/304378.htm

http://developer.51cto.com/art/201111/304387.htm

http://my.oschina.net/u/557580/blog/225554

http://chenzehe.iteye.com/blog/1762893


https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html


http://www.journaldev.com/2377/java-lock-example-and-concurrency-lock-vs-synchronized

 

 

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值