当前位置:网站首页>《利用CAS实现自旋锁》
《利用CAS实现自旋锁》
2022-06-23 15:25:00 【weixin_43766298】
1.自旋锁概念
- 自旋锁(spinlock):是指当一个线程在获取锁的时候,如果锁已经被其它线程获取,那么该线程将循环等待,然后不断的判断锁是否能够被成功获取,直到获取到锁才会退出循环。
2.编写自旋锁
import java.util.concurrent.atomic.AtomicBoolean;
//自旋锁
public class SpinLock {
//CAS
private AtomicBoolean cas = new AtomicBoolean(false);
//锁持有者线程
private Thread spinLockOwnerThread = null;
public void lock(){
while (!cas.compareAndSet(false,true))
{
//CAS自旋
}
spinLockOwnerThread = Thread.currentThread();
}
public void unLock(){
//当前线程等于锁持有着线程才允许解锁
if (Thread.currentThread()==spinLockOwnerThread)
{
spinLockOwnerThread = null;
cas.set(false);
//最后设置CAS的值为false,为什么?
//如果先执行cas.set(false),此时又来了一个新的线程,
//成功过了CAS的compareAndSet,
//将 spinLockOwnerThread 指向Thread.currentThread();
//最后再执行spinLockOwnerThread = null;
//这会导致最后持有锁的线程无法解锁
//也就是无法通过Thread.currentThread()==spinLockOwnerThread判断
//从而导致程序一直阻塞
}
}
}
3.测试自旋锁
class Test {
//引入自旋锁
private static final SpinLock spinLock = new SpinLock();
public static void main(String[] args) {
Thread t1 = new Thread(Test::run,"t1");
Thread t2 = new Thread(Test::run,"t2");
Thread t3 = new Thread(Test::run,"t3");
Thread t4 = new Thread(Test::run,"t4");
t1.start();
t2.start();
t3.start();
t4.start();
}
public static void run(){
spinLock.lock();
System.out.println(Thread.currentThread().getName());
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
spinLock.unLock();
}
}
}
边栏推荐
- MQ消息中间件理论详解
- VIM backup history command
- TCP协议三次握手和四次挥手抓包分析
- This year's cultural entertainers have turned their sidelines into their main business
- golang 重要知识:sync.Cond 机制
- stylegan3:alias-free generative adversarial networks
- 苹果 iPhone、三星手机等电子产品开始经平行进口渠道进入俄罗斯
- MIPI C-PHY协议你了解吗?手机高速接口之一
- 英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化
- Arrays in JS
猜你喜欢

stylegan2:analyzing and improving the image quality of stylegan

js的slice()和splice()

Top 10 purchase, sales and inventory software rankings!

golang 重要知识:RWMutex 读写锁分析

Jsr303 data verification
Explain in detail the principle and implementation of redis distributed lock

任何代码未动的情况下第二天项目访问速度明显下降,案例分析

Matlab| sparse auxiliary signal denoising and pattern recognition in time series data

Stone from another mountain - Intelligent Question and answer technology in wechat search

Important knowledge of golang: mutex
随机推荐
PHP 2D array insert
golang 重要知识:atomic 原子操作
变压器只能转换交流电,那直流电怎么转换呢?
嵌入式软件架构设计-程序分层
Explain in detail the principle and implementation of redis distributed lock
B. AND 0, Sum Big-Codeforces Round #716 (Div. 2)
Introduction to the push function in JS
139. 单词拆分
golang 重要知识:sync.Once 讲解
JS垃圾回收
子级文件拖到上一级
The meaning of FPGA abbreviations and words in engineering field
MIPI C-PHY协议你了解吗?手机高速接口之一
图片保存:torchvision.utils.save_image(img, imgPath)
Big factory Architect: how to draw a grand business map?
基因检测,如何帮助患者对抗疾病?
Important knowledge of golang: waitgroup parsing
Which platform is a good place to open a futures account? Is it safe to open an online futures account?
Detailed steps for MySQL dual master configuration
VGG下载(.net文件和imagenet-vgg-verydeep-19)