8.java锁之自旋锁代码验证

2022/4/11 1:12:55

本文主要是介绍8.java锁之自旋锁代码验证,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package com.mydemo;

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

import static java.lang.Thread.currentThread;

public class SpinLockDemo {

    AtomicReference<Thread> atomicReference=new AtomicReference<>();



    public void myLock(){
        Thread thread =  Thread.currentThread();
        System.out.println(currentThread().getName()+"\t come in!");

        while (!atomicReference.compareAndSet(null,thread))
        {

        }



    }

    public void myUnlock(){

        Thread thread = Thread.currentThread();
        atomicReference.compareAndSet(thread,null);
        System.out.println(currentThread().getName()+"\t invoke myUnlock!");
    }
    public static void main(String[] args) {
        SpinLockDemo spinLockDemo = new SpinLockDemo();
        new Thread(()->{
            spinLockDemo.myLock();
            try {
                            TimeUnit.SECONDS.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
            spinLockDemo.myUnlock();


        },"t1").start();
        try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

        // 1秒后,启动t2线程,开始占用这个锁
        new Thread(() -> {

            // 开始占有锁
            spinLockDemo.myLock();
            // 开始释放锁
            spinLockDemo.myUnlock();

        }, "t2").start();


    }
}

 



这篇关于8.java锁之自旋锁代码验证的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程