当前位置:网站首页>《利用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();
}
}
}
边栏推荐
- MySQL series: overview of the overall architecture
- 力扣每日一题-第25天-495.提莫攻击
- Convert JSON file of labelme to coco dataset format
- Important knowledge of golang: sync Once explanation
- 快速排序的簡單理解
- Variable declaration of go language
- golang 重要知识:sync.Cond 机制
- List query sorting parameter processing
- Explore the "store" on the cloud. The cloud store is newly upgraded!
- JS traversal array (using the foreach () method)
猜你喜欢

Diffraction of light

stylegan2:analyzing and improving the image quality of stylegan

js的slice()和splice()

Summary of operating system underlying knowledge (interview)

Arrays in JS

js中的push函数介绍

The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service

任何代码未动的情况下第二天项目访问速度明显下降,案例分析
The idea and method of MySQL master-slave only synchronizing some libraries or tables

A transformer can only convert alternating current. How can I convert direct current?
随机推荐
列表查询排序参数处理
Horizon development board commissioning
PHP 2D array insert
Important knowledge of golang: sync Cond mechanism
List query sorting parameter processing
139. 单词拆分
Origin of sectigo (Comodo) Certificate
Explain in detail the principle and implementation of redis distributed lock
ABP framework - data access infrastructure (Part 2)
CAS操作在ARM和x86下的不同实现
Gartner最新报告:低代码应用开发平台在国内的发展
MySQL series: storage engine
ABP框架之——数据访问基础架构(下)
JS创建一个数组(字面量)
[opencv450] salt and pepper noise demo
The work and development steps that must be done in the early stage of the development of the source code of the live broadcasting room
matlab: 如何从一些数据里知道是由哪些数据相加得出一个已知数
B. Integers Shop-Hello 2022
The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service
看,这就是调制解调原理分析!附仿真文件