Java多线程——使用CAS实现自旋锁

使用JUC的原子引用类的CAS特性实现自旋锁

直接上代码

package com.leolee.multithreadProgramming.test.spinLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName SpinLockTest
 * @Description: 自定义自旋锁
 * @Author LeoLee
 * @Date 2021/3/1
 * @Version V1.0
 **/
public class SpinLockTest {

    AtomicReference atomicReference = new AtomicReference<>();

    public void myLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "come in");
        while (!atomicReference.compareAndSet(null, thread)) {

        }
    }

    public void myUnlock() {
        Thread thread = Thread.currentThread();
        atomicReference.compareAndSet(thread, null);
        System.out.println(Thread.currentThread().getName() + "unlock");
    }

    public static void main(String[] args) {
        SpinLockTest spinLockTest = new SpinLockTest();

        new Thread(() -> {
            spinLockTest.myLock();
            System.out.println("t1 get lock");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            spinLockTest.myUnlock();
        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);//保证t1一定是先启动,先持有锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            spinLockTest.myLock();

            System.out.println("t2 get lock");

            spinLockTest.myUnlock();
        }, "t2").start();
    }
}


执行结果:
t1come in
t1 get lock
t2come in
t1unlock
t2 get lock
t2unlock

 

你可能感兴趣的:(#,JAVA多线程并发编程,CAS,AtomicReference)