当前位置:网站首页>Spin lock using CAS
Spin lock using CAS
2022-06-23 16:05:00 【weixin_ forty-three million seven hundred and sixty-six thousan】
utilize CAS Spin lock
1. Spin lock concept
- spinlocks (spinlock): When a thread is acquiring a lock , If the lock has been acquired by another thread , Then the thread will wait in a loop , And then constantly judge whether the lock can be successfully acquired , Loop will not exit until lock is acquired .
2. Write spin lock
import java.util.concurrent.atomic.AtomicBoolean;
// spinlocks
public class SpinLock {
//CAS
private AtomicBoolean cas = new AtomicBoolean(false);
// Lock holder thread
private Thread spinLockOwnerThread = null;
public void lock(){
while (!cas.compareAndSet(false,true))
{
//CAS The spin
}
spinLockOwnerThread = Thread.currentThread();
}
public void unLock(){
// The current thread is equal to the thread that holds the lock before unlocking
if (Thread.currentThread()==spinLockOwnerThread)
{
spinLockOwnerThread = null;
cas.set(false);
// The last set CAS The value of is false, Why? ?
// If you execute first cas.set(false), Here comes a new thread ,
// It's been a success CAS Of compareAndSet,
// take spinLockOwnerThread Point to Thread.currentThread();
// Finally, we will carry out spinLockOwnerThread = null;
// This will cause the last thread holding the lock to fail to unlock
// That is to say, it is impossible to pass through Thread.currentThread()==spinLockOwnerThread Judge
// This causes the program to block all the time
}
}
}
3. Test the spin lock
class Test {
// Introduce spin lock
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();
}
}
}
边栏推荐
- Build vscode into an invincible IDE (14) tasks JSON and launch JSON configuration details, add automation tasks at will
- ABP framework - data access infrastructure (Part 2)
- Three simple tips for accelerating yarn install
- get_edges
- 中大人脸素描FERET数据库(CUFSF)
- Does the enterprise want to use the MES system? These conditions have to be met
- 【TcaplusDB知识库】Tmonitor单机安装指引介绍(二)
- 139. 單詞拆分
- [tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
- 进阶开发- 泛型入门基础类测试
猜你喜欢

js 递归json树 根据 子id 查 父id

This year's cultural entertainers have turned their sidelines into their main business

window远程桌面连接互传文件加速小技巧

PageHelper faces the paging problem of complex service data processing

解决:在验证阶段,第一个batch不会报错,第二个batch报cuda超出的错误

matlab: 如何从一些数据里知道是由哪些数据相加得出一个已知数

【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)

MySQL transactions and locks

Important knowledge of golang: detailed explanation of context

513. Find Bottom Left Tree Value
随机推荐
Example of if directly judging data type in JS
Advanced development stage - the thickening of potential suspension wire begins A small step now, a big step next year
Different implementation of CAS operation under arm and x86
B. AND 0, Sum Big-Codeforces Round #716 (Div. 2)
pytorch:模型的保存与导出
stylegan2:analyzing and improving the image quality of stylegan
513. Find Bottom Left Tree Value
服务器的部署及使用说明
B. Integers Shop-Hello 2022
自监督学习(SSL)Self-Supervised Learning
stylegan3:alias-free generative adversarial networks
Summarize the experience of purchasing Alibaba cloud servers
PageHelper faces the paging problem of complex service data processing
任何代码未动的情况下第二天项目访问速度明显下降,案例分析
积分商城要如何做才能获取到利润
Charge pump principle handout, how is the voltage "pumped"?
子级文件拖到上一级
MySQL transactions and locks
批量注册组件
If no code is moved, the project access speed drops significantly the next day. Case analysis