ReentrantLock 源代码_TechQ 知识库 线程安全(可以看成一个操作,并且是在多处理器的环境下安全),而且效率极高,因为现代CPU几乎都支持 CAS (比较并交换,compare and swap)指令,这个方法最终是由一个native 方法执行CAS 如果原子操作尝试失败,说明锁被其他线程获得
基于1个网页-相关网页
比较并交换(CAS)。
在Intel处理器中,比较并交换通过指令的cmpxchg系列实现。
On Intel processors, compare-and-swap is implemented by the CMPXCHG family of instructions.
在表面之下,原子变量的操作会变为平台提供的用于并发访问的硬件原语,比如比较并交换。
Under the hood, operations on atomic variables get turned into the hardware primitives that the platform provides for concurrent access, such as compare-and-swap.
应用推荐