自旋锁是采用让当前线程不停地的在循环体内执行实现的,当循环的条件被其他线程改变时 才能进入临界区,线程尝试获取锁的过程不会阻塞;
1. 如果某个线程持有锁的时间过长,就会导致其它等待获取锁的线程进入循环等待,消耗 CPU。使用不当会造成CPU使用率极高。
2. 可能会带来“线程饥饿”的情况;
package com.xxx.lcloud.study; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; /** * Created by hinson on 2020-5-19. */ public class SpinLock { AtomicReference<Thread> atomicReference = new AtomicReference<>(); public static void main(String[] args) { SpinLock spinLock = new SpinLock(); new Thread(()->{ spinLock.lock(); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } spinLock.unLock(); },"线程A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ spinLock.lock(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } spinLock.unLock(); },"线程B").start(); } private void lock(){ Thread thread = Thread.currentThread(); //准备抢占 while(!atomicReference.compareAndSet(null,thread)){ //空轮询 } } private void unLock(){ Thread thread = Thread.currentThread(); atomicReference.compareAndSet(thread,null); } } 复制代码