- interface Account {
- // 获取余额
- Integer getBalance();
-
- // 取款
- void withdraw(Integer amount);
-
- /**
- * 方法内会启动 1000 个线程,每个线程做 -10 元 的操作
- * 如果初始余额为 10000 那么正确的结果应当是 0
- */
- static void demo(Account account) {
- List
ts = new ArrayList<>(); - for (int i = 0; i < 1000; i++) {
- ts.add(new Thread(() -> {
- account.withdraw(10);
- }));
- }
- long start = System.nanoTime();
- ts.forEach(Thread::start);
- ts.forEach(t -> {
- try {
- t.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- });
- long end = System.nanoTime();
- System.out.println(account.getBalance()
- + " cost: " + (end-start)/1000_000 + " ms");
- }
- }
class AccountUnsafe implements Account { private Integer balance; public AccountUnsafe(Integer balance) { this.balance = balance; } @Override public Integer getBalance() { return this.balance; } @Override public void withdraw(Integer amount) { this.balance -= amount; } }测试:
public class TestAccount { public static void main(String[] args) { Account account = new AccountUnsafe(10000); Account.demo(account); } }
- class AccountUnsafe implements Account {
-
- private Integer balance;
-
- public AccountUnsafe(Integer balance) {
- this.balance = balance;
- }
-
- @Override
- public Integer getBalance() {
- synchronized (this) {
- return this.balance;
- }
- }
-
- @Override
- public void withdraw(Integer amount) {
- synchronized (this) {
- this.balance -= amount;
- }
- }
- }
- class AccountCas implements Account {
- private AtomicInteger balance;
-
- public AccountCas(int balance) {
- this.balance = new AtomicInteger(balance);
- }
-
- @Override
- public Integer getBalance() {
- return balance.get();
- }
-
- @Override
- public void withdraw(Integer amount) {
- while(true) {
- // 获取余额的最新值
- int prev = balance.get();
- // 要修改的余额
- int next = prev - amount;
- // 真正修改
- if(balance.compareAndSet(prev, next)) {
- break;
- }
- }
- }
- }
AtomicInteger 的解决方法,内部并没有用锁来保护共享变量的线程安全。
其中的关键是 compareAndSet ,它的简称就是 CAS (也有 Compare And Swap 的说法),它必须是原子操作。
注意其实 CAS 的底层是 lock cmpxchg 指令( X86 架构),在单核 CPU 和多核 CPU 下都能够保证【比较 -交换】的原子性。
(1)获取共享变量时,为了保证该变量的可见性,需要使用 volatile 修饰。
(2)它可以用来修饰成员变量和静态成员变量,他可以避免线程从自己的工作缓存中查找变量的值,必须到主存中获取它的值,线程操作 volatile 变量都是直接操作主存。即一个线程对 volatile 变量的修改,对另一个线程可见。
注意:
volatile 仅仅保证了共享变量的可见性,让其他线程能够看到最新值,但不能解决指令交错问题(不能保证原子性)。
CAS 必须借助 volatile 才能读取到共享变量的最新值来实现【比较并交换】的效果。
无锁情况下,即使重试失败,线程始终在高速运行,没有停歇,而 synchronized 会让线程在没有获得锁的时候,发生上下文切换,进入阻塞。打个比喻:
(1)线程就好像高速跑道上的赛车,高速运行时,速度超快,一旦发生上下文切换,就好比赛车要减速、熄火,等被唤醒又得重新打火、启动、加速... 恢复到高速运行,代价比较大。
(2)但无锁情况下,因为线程要保持运行,需要额外 CPU 的支持,CPU 在这里就好比高速跑道,没有额外的跑道,线程想高速运行也无从谈起,虽然不会进入阻塞,但由于没有分到时间片,仍然会进入可运行状态,还是会导致上下文切换。
结合 CAS 和 volatile 可以实现无锁并发,适用于线程数少、多核 CPU 的场景下。
(1)CAS 是基于乐观锁的思想:最乐观的估计,不怕别的线程来修改共享变量,就算改了也没关系,我吃亏点再重试呗。
(2)synchronized 是基于悲观锁的思想:最悲观的估计,得防着其它线程来修改共享变量,我上了锁你们都别想改,我改完了解开锁,你们才有机会。
(3)CAS 体现的是无锁并发、无阻塞并发:
1️⃣因为没有使用 synchronized,所以线程不会陷入阻塞,这是效率提升的因素之一。
2️⃣但如果竞争激烈,可以想到重试必然频繁发生,反而效率会受影响。