大家好,我是walker
一个从文科自学转行的程序员~
爱好编程,偶尔写写编程文章和生活
欢迎关注公众号【I am Walker】,回复“电子书”,就可以获得200多本编程相关电子书哈~
我的gitee:https://gitee.com/shen-chuhao/walker.git 里面很多技术案例!
自旋锁(spinLock)
是指尝试获取锁的线程不会阻塞,而是采用循环的方式获取锁
好处:减少上下文切换的消耗坏处:循环会消耗CPU
案例
package synchronizedTest.spinLock;
import java.util.concurrent.atomic.AtomicReference;
/**
* author:walker
* time: 2022/3/21
* description: 自定义自旋锁
*/
public class SpinLockTest {
/**
* 1.AtomicReference和AtomicInteger非常类似,不同之处就在于AtomicInteger是对整数的封装,而AtomicReference则对应普通的对象引用。也就是它可以保证你在修改对象引用时的线程安全性。
* 2.AtomicReference是作用是对”对象”进行原子操作。 提供了一种读和写都是原子性的对象引用变量。原子意味着多个线程试图改变同一个AtomicReference(例如比较和交换操作)将不会使得AtomicReference处于不一致的状态。
*/
AtomicReference<Thread> atomicReference=new AtomicReference<>();
public void lock(){
Thread thread = Thread.currentThread();
System.out.println(thread.getName()+"加锁");
/**
* CAS:public final boolean compareAndSet(V expect, V update)
* 如果atomicReference的值等于期待的null,则将结果设置为thread
* 主要是比较当前的值和期待值,如果一致则修改,否则则不修改
*/
/**
* 如果当前的原子引用不等于null,则一直循环,相当于自旋,如果等于null的话,就终止循环,表示锁被人占用
*/
while(!atomicReference.compareAndSet(null,thread)){
System.out.println(thread.getName()+"自旋中");
}
}
public void unlock(){
Thread thread = Thread.currentThread();
/**
* 如果原子引用的线程和当前的对象一致,则设置原子引用为null,相当于释放锁
*/
atomicReference.compareAndSet(thread,null);
System.out.println(thread.getName()+"释放锁");
}
public static void main(String[] args) throws InterruptedException {
SpinLockTest spinLockTest = new SpinLockTest();
//定义线程A
new Thread(()->{
spinLockTest.lock();
try {
//这里让线程休眠,但是不让线程释放锁,作用是让线程B进行等待自旋
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
spinLockTest.unlock();
},"线程A").start();
//定义线程B
new Thread(()->{
spinLockTest.lock();
spinLockTest.unlock();
},"线程B").start();
}
}