首页 > 其他 > 详细

自旋锁CAS(compareAndSet)

时间:2021-04-30 17:50:52      阅读:33      评论:0      收藏:0      [点我收藏+]
package com.atguigu.boot.com.atguigu;

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

public class SpinLockDemo {
    //原子引用线程
    AtomicReference<Thread> atomicReference=new AtomicReference<>();

    public void myLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"\t come in myLockO(∩_∩)O");
//        while(!atomicReference.compareAndSet(null,thread)){
//            System.out.println("");
//        }
        do{
            //System.out.println(Thread.currentThread().getName()+"线程正在尝试获取");
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }while(!atomicReference.compareAndSet(null,thread));

    }

    public void myUnLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"\t come in myUnLockO(∩_∩)O");
        atomicReference.compareAndSet(thread,null);
    }

    public static void main(String[] args) {
        SpinLockDemo spinLockDemo=new SpinLockDemo();
        new Thread(()->{
            spinLockDemo.myLock();
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            spinLockDemo.myUnLock();
        },"AA").start();

        try {
            TimeUnit.SECONDS.sleep(1) ;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            spinLockDemo.myLock();
            try {
                TimeUnit.SECONDS.sleep(1) ;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            spinLockDemo.myUnLock();
        },"BB").start();

    }
}

 

package com.atguigu.boot.com.atguigu;

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

public class SpinLockDemo {
//原子引用线程
AtomicReference<Thread> atomicReference=new AtomicReference<>();

public void myLock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+"\t come in myLockO(∩_∩)O");
// while(!atomicReference.compareAndSet(null,thread)){
// System.out.println("");
// }
do{
//System.out.println(Thread.currentThread().getName()+"线程正在尝试获取");
// try {
// TimeUnit.SECONDS.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}while(!atomicReference.compareAndSet(null,thread));

}

public void myUnLock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+"\t come in myUnLockO(∩_∩)O");
atomicReference.compareAndSet(thread,null);
}

public static void main(String[] args) {
SpinLockDemo spinLockDemo=new SpinLockDemo();
new Thread(()->{
spinLockDemo.myLock();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
spinLockDemo.myUnLock();
},"AA").start();

try {
TimeUnit.SECONDS.sleep(1) ;
} catch (InterruptedException e) {
e.printStackTrace();
}

new Thread(()->{
spinLockDemo.myLock();
try {
TimeUnit.SECONDS.sleep(1) ;
} catch (InterruptedException e) {
e.printStackTrace();
}
spinLockDemo.myUnLock();
},"BB").start();

}
}

自旋锁CAS(compareAndSet)

原文:https://www.cnblogs.com/ffzzcommsoft/p/14722643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!