wait-free、lock-free、lock-base
If a program is lock-free, it basically means that at least one of its threads is guaranteed to make progress over an arbitrary period of time. If a program deadlocks, none of its threads (and therefore the program as a whole) cannot make progress - we can say it's not lock-free. Since lock-free programs are guaranteed to make progress, they are guaranteed to complete (assuming finite execution without exceptions).
Wait-free is a stronger condition which means that every thread is guaranteed to make progress over an arbitrary period of time, regardless of the timing/ordering of thread execution; and so we can say that the threads finish independently. All wait-free programs are lock-free.
I don't know offhand of any Java examples which illustrate this but I can tell you that lock-free/wait-free programs are typically implemented without locks, using low-level primitives such as CAS instructions.
所谓「无锁数据结构」,是不是可以理解为本质上并不是「无锁」,而只是锁定粒度降到了最低?
1. 物理层面的“真互斥” vs 软件层面的“非阻塞” 你的直觉非常准。在最底层的硬件实现上,CPU 执行 CAS 等原子指令时,确实会触发“总线锁”(Bus Lock)或者“缓存行锁定”(Cache Line Locking,比如 MESI 协议)。从这个物理层面来说,原子量绝对是一种极小粒度的、机器字级别的“排他锁”。 但为什么并发理论依然叫它 Lock-free?因为在软件工程里,“锁”的本质特征不是互斥,而是“阻塞(Blocking)”。传统锁允许一个线程拿着资源去睡觉,把别人全卡死;而 Lock-free 靠着原子指令,保证了无论如何系统里总有人能往前走。它戒掉的是“阻塞和死锁”,而不是“互斥”。
2. “砖块”与“监狱”的区别 你提到原子量能用来实现自旋锁,所以它本身也是锁。这个逻辑其实混淆了“原语(Primitive)”和“构造物(Construct)”。 原子操作就像是一块极其坚固的砖头。它本身只有一个瞬间的动作(比较并交换)。 如果你用一个
while(!CAS(…))把一段长代码包起来,非要等条件满足才放行,那是你用这块砖头砌成了一座名为“自旋锁”的监狱。砖头本身不是监狱,是你的代码逻辑(无限循环等待)赋予了它“锁”的压迫感。无锁数据结构之所以无锁,是因为它严格限制了这块砖的用法,绝不砌墙。

浙公网安备 33010602011771号