⑥. 自旋锁
- ①. 什么是自旋锁?
(是指尝试获取锁的线程不会立即阻塞,而是采用循环的方式去尝试获取锁,当线程发
现锁被占用时,会不断循环判断锁的状态,直到获取。这样的好处是减少线程上
下文切换的消耗,缺点是循环会消耗CPU)
②. 如何手写一个自选锁
//自旋锁 public class AtomicReferenceThreadDemo { static AtomicReference<Thread>atomicReference=new AtomicReference<>(); static Thread thread; public static void lock(){ thread=Thread.currentThread(); System.out.println(Thread.currentThread().getName()+"\t"+"coming....."); while(!atomicReference.compareAndSet(null,thread)){ } } public static void unlock(){ System.out.println(Thread.currentThread().getName()+"\t"+"over....."); atomicReference.compareAndSet(thread,null); } public static void main(String[] args) { new Thread(()->{ AtomicReferenceThreadDemo.lock(); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) {e.printStackTrace();} AtomicReferenceThreadDemo.unlock(); },"A").start(); new Thread(()->{ AtomicReferenceThreadDemo.lock(); AtomicReferenceThreadDemo.unlock(); },"B").start(); } }
- ③. CAS缺点
- 循环时间长开销很大
- 引出来ABA问题(在CAS篇章将详细说明)