【多线程开发 4】从源码学习LockSupport

从源码学习LockSupport

2024年6月30日

大家好啊,好久没写博客了,今天打算写一下,讲一下JUC里面LockSupport这个类。

这个是一个工具类,实际上也是为了线程通信开发的。它的源码比较短,也只引用了Unsafe一个类。所以我们可以先看下他的源码:

类注释

他的类注释如下:

Basic thread blocking primitives for creating locks and other synchronization classes.
This class associates, with each thread that uses it, a permit (in the sense of the Semaphore class). A call to park will return immediately if the permit is available, consuming it in the process; otherwise it may block. A call to unpark makes the permit available, if it was not already available. (Unlike with Semaphores though, permits do not accumulate. There is at most one.) Reliable usage requires the use of volatile (or atomic) variables to control when to park or unpark. Orderings of calls to these methods are maintained with respect to volatile variable accesses, but not necessarily non-volatile variable accesses.
Methods park and unpark provide efficient means of blocking and unblocking threads that do not encounter the problems that cause the deprecated methods Thread. suspend and Thread. resume to be unusable for such purposes: Races between one thread invoking park and another thread trying to unpark it will preserve liveness, due to the permit. Additionally, park will return if the caller’s thread was interrupted, and timeout versions are supported. The park method may also return at any other time, for “no reason”, so in general must be invoked within a loop that rechecks conditions upon return. In this sense park serves as an optimization of a “busy wait” that does not waste as much time spinning, but must be paired with an unpark to be effective.
The three forms of park each also support a blocker object parameter. This object is recorded while the thread is blocked to permit monitoring and diagnostic tools to identify the reasons that threads are blocked. (Such tools may access blockers using method getBlocker(Thread).) The use of these forms rather than the original forms without this parameter is strongly encouraged. The normal argument to supply as a blocker within a lock implementation is this.
These methods are designed to be used as tools for creating higher-level synchronization utilities, and are not in themselves useful for most concurrency control applications. The park method is designed for use only in constructions of the form:

while (!canProceed()) {   
   // ensure request to unpark is visible to other threads 
   ...   
       LockSupport. park(this);
}

where no actions by the thread publishing a request to unpark, prior to the call to park, entail locking or blocking. Because only one permit is associated with each thread, any intermediary uses of park, including implicitly via class loading, could lead to an unresponsive thread (a “lost unpark”).
Sample Usage. Here is a sketch of a first-in-first-out non-reentrant lock class:

class FIFOMutex {
   private final AtomicBoolean locked = new AtomicBoolean(false);
   private final Queue<Thread> waiters = new ConcurrentLinkedQueue<>();

   public void lock() {
       boolean wasInterrupted = false;
       // publish current thread for unparkers
       waiters.add(Thread.currentThread());
       // Block while not first in queue or cannot acquire lock
       while (waiters.peek() != Thread.currentThread() || !locked.compareAndSet(false, true)) {
           LockSupport.park(this);
           // ignore interrupts while waiting       
           if (Thread.interrupted()) wasInterrupted = true;
       }
       waiters.remove();
       // ensure correct interrupt status on return
       if (wasInterrupted)
           Thread.currentThread().interrupt();
   }

   public void unlock() {
       locked.set(false);
       LockSupport.unpark(waiters.peek());
   }

   static {     
       // Reduce the risk of "lost unpark" due to classloading
       Class<?> ensureLoaded = LockSupport.class;
   }
}

第一段文本大意为:

用于创建锁和其他同步类的基本线程阻塞原语。这个类与使用它的每个线程关联一个许可证(在信号量类的意义上)。如果许可证可用,将立即返回停车呼叫,并在此过程中消耗许可证;否则可能会堵塞。如果许可证尚未可用,则调用取消停车使其可用。(但与信号量不同的是,许可证不会累积。最多只有一个。)可靠的使用需要使用易失性(或原子)变量来控制何时停放或取消停放。调用这些的顺序

其实就是对想要枷锁的信息加上一个许可,提前给他许可再消费许可,还是代码走到消费许可后再拿到许可都可以,。

第二段文本大意为:

如果在调用park之前,线程没有发布请求取消park的操作,则会导致锁定或阻塞。因为每个线程只关联一个许可证,任何中间使用park(包括通过类加载隐式地使用)都可能导致线程无响应(“丢失的unpark”)。
样品使用。这是一个先进先出的不可重入锁类的草图:

就是根据第二块代码展示LockSupport具体怎么使用。

代码细节

unpark(Thread thread)

给对应thread发放许可。如果线程被LocjSupport的park()方法阻塞时,使用该方法会被立刻释放运行,如果没有阻塞,则使用该方法发放许可证明时,在下一次调用park()方法,则会直接运行不被阻塞。

park(Object blocker)

会向当前线程请求许可,没有许可就会阻塞,除非发生以下三种情况之一:

  • 其他线程以当前线程为目标调用unpark
  • 其他线程中断当前线程
  • 调用返回

但是park方法不能看出来是哪一个原因导致线程可以重新运行。需要调用的人自己写代码看,例如,线程返回时的中断状态。

parkNanos(Object blocker, long nanos)

和park方法一样,但是有时间限制,到了时间限制也会停止阻塞。

getBlocker(Thread t)

提供阻塞线程t的正在被阻塞的park方法是谁调用的,但是线程不安全,拿到这个对象操作的时候可能也已经被unpark了也说不定。

park()

直接禁用当前线程。

private LockSupport() {} // Cannot be instantiated.

LockSupport不能被初始化,只能使用LockSupport进行开发

setCurrentBlocker(Object blocker)

该方法可以做到在blocker使用park()方法之前会调用该方法,可以做到一些分析的作用。

总结

这个方法比较简单,但是比较方便进行线程间通信。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/760481.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

HarmonyOS Next开发学习手册——单选框 (Radio)

Radio是单选框组件&#xff0c;通常用于提供相应的用户交互选择项&#xff0c;同一组的Radio中只有一个可以被选中。具体用法请参考 Radio 。 创建单选框 Radio通过调用接口来创建&#xff0c;接口调用形式如下&#xff1a; Radio(options: {value: string, group: string})…

Linux常用工具使用方式

目录 常用工具&#xff1a; 安装包管理工具&#xff1a; 查找含有关键字的软件包 安装软件 安装文件传输工具 安装编辑器 C语言编译器 C编译器 安装调试器 安装项目版本管理工具 cmake 卸载软件 安装jsoncpp 安装boost库 安装mariadb 安装tree&#xff08;让目录…

Python28-3 朴素贝叶斯分类算法

朴素贝叶斯算法简介 朴素贝叶斯&#xff08;Naive Bayes&#xff09;算法是一种基于贝叶斯定理的分类算法。它广泛应用于文本分类、垃圾邮件检测和情感分析等领域。该算法假设特征之间是独立的&#xff0c;这个假设在实际情况中可能并不完全成立&#xff0c;但Naive Bayes在许…

java笔记(30)——反射的 API 及其 使用

文章目录 反射1. 什么是反射2. 获取class字段&#xff08;字节码文件对象&#xff09;方式1方式2方式3应用 3. 获取构造方法和权限修饰符前期准备获取所有的公共构造方法获取所有的构造方法获取无参构造方法获取一个参数的构造方法获取一个参数的构造方法获取两个参数的构造方法…

Java面试题--JVM大厂篇之G1 GC的分区管理方式如何减少应用线程的影响

目录 引言: 正文: 1. 区域划分&#xff08;Region&#xff09; 2. 并行和并发回收 3. 区域优先回收&#xff08;Garbage First&#xff09; 4. 可预测的停顿时间 5. 分阶段回收 6. 复制和压缩 实际效果: 场景举例 1. 减少单次GC的影响 2. 支持高并发环境 3. 优…

数学建模(1):期末大乱炖

1 概述&#xff01;&#xff01; 1.1 原型和模型 原型&#xff1a;客观存在的研究对象称为原型&#xff0c;也称为“系统”、“过程”。 机械系统、电力系统、化学反应过程、生产销售过程等都是原型&#xff1b; 研究原型的结构和原理&#xff0c; 从而进行优化、预测、评价…

一区算法MPA|海洋捕食者算法原理及其代码实现(Matlab/Python))

Matlab/Python&#xff1a; 本文KAU将介绍一个2020年发表在1区期刊ESWA上的优化算法——海洋捕食者算法 (Marine Predators Algorithm&#xff0c;MPA)[1] 该算法由Faramarzi等于2020年提出&#xff0c;其灵感来源于海洋捕食者之间不同的觅食策略、最佳相遇概率策略、海洋记…

【MySQL】Linux下MySQL的目录结构、用户、权限与角色

一、Linux下MySQL的目录结构 1、MySQL相关目录 数据库文件存放路径&#xff1a;/var/lib/mysql数据库命令存放路径&#xff1a;/user/bin和/user/sbin配置文件目录&#xff1a;/usr/share/mysql-8.0/、/usr/share/mysql/和/etc/my.cnf 2、假设我们创建了一个数据库dbtest1&a…

使用evo工具比较ORB-SLAM3的运行轨迹(从安装到解决报错)

ORB-SLAM2和ORB-SLAM3怎么跑出来&#xff0c;之前都有相关的保姆级的教程&#xff0c;下来给大家介绍一款evo工具&#xff0c;给科研加速&#xff01;&#xff01;&#xff01; 文章目录 1.下载evo2.生成轨迹3.evo别的功能使用 1.下载evo 输入命令下载 pip install -i https…

你真的会udf提权???数据库权限到系统权限 内网学习 mysql的udf提权操作 ??msf你会用了吗???

我们在已经取得了数据库的账号密码过后&#xff0c;我们要进一步进行提取的操作&#xff0c;我们mysql有4钟提权的操作。 udf提权(最常用的)mof提权启动项提权反弹shell提权操作 怎么获取密码操作&#xff1a; 怎么获取密码&#xff0c;通过sql注入获取这个大家都应该知道了&a…

百强韧劲,进击新局 2023年度中国医药工业百强系列榜单发布

2024年&#xff0c;经济工作坚持稳中求进、以进促稳、先立后破等工作要求。医药健康行业以不懈进取的“韧劲”&#xff0c;立身破局&#xff0c;迎变启新。通过创新和迭代应对不确定性&#xff0c;进化韧性力量&#xff0c;坚持高质量发展&#xff0c;把握新时代经济和社会给予…

零基础开始学习鸿蒙开发-读书app简单的设计与开发

目录 1.首页设计 2.发现页面的设计 3.设置页面的设计 4.导航页设计 5.总结&#xff1a; 6.最终的效果 1.首页设计 Entry Component export struct home {State message: string 首页build() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.B…

基于线调频小波变换的非平稳信号分析方法(MATLAB)

信号处理领域学者为了改进小波变换在各时频区间能量聚集性不高的缺点&#xff0c;有学者在小波分析基础上引入调频算子构成了线性调频小波变换&#xff0c;线调频小波一方面继承了小波变换的理论完善性&#xff0c;另一方面用一个新的参数&#xff08;线调频参数&#xff09;刻…

构建高效业财一体化管理体系

构建高效业财一体化管理体系 业财一体化战略意义 提升决策质量 强化数据支撑&#xff1a;通过整合业务与财务数据&#xff0c;为决策提供准确、实时的信息基础&#xff0c;确保分析的深度与广度。促进业务与财务协同&#xff1a;打破信息孤岛&#xff0c;实现业务流程与财务管…

Django 定义模型执行迁移

1&#xff0c;创建应用 Test/app8 python manage.py startapp app8 2&#xff0c;注册应用 Test/Test/settings.py 3&#xff0c;配置路由 Test/Test/urls.py from django.contrib import admin from django.urls import path, includeurlpatterns [path(app8/, include(a…

Linux服务器上安装CUDA11.2和对应的cuDNN 8.4.0

一、检查 检查本机是否有CUDA工具包&#xff0c;输入nvcc -V: 如图所示&#xff0c;服务器上有CUDA&#xff0c;但版本为9.1.85&#xff0c;版本过低&#xff0c;因此博主要重装一个新的。 二、安装CUDA 1.查看服务器最高支持的CUDA版本 在命令行输入nvidia-smi查看显卡驱动…

Mining Engineering First Aid Riding

4个最主要的日常技能&#xff1a;Mining 采矿 Engineering 工程 First Aid 急救 Riding 骑术 4个最主要的日常技能

C# 信号量的使用

学习来源&#xff1a;《.net core 底层入门》 第六章第9节&#xff1a;信号量 案例&#xff1a;主线程负责添加数据&#xff0c;子线程负责获取数据 使用SemaphoreSlim&#xff08;轻信号量&#xff09;实现&#xff1a; using System; using System.Collections.Generic; us…

AI写作变现指南:从项目启动到精通

项目启动 1. 确定目标客户群体 首先&#xff0c;明确谁是我们的目标客户。以下是一些潜在的客户群体&#xff1a; 大学生&#xff1a;他们需要写论文、报告、演讲稿等。 职场人士&#xff1a;包括需要撰写商业计划书、市场分析报告、项目提案等的专业人士。 自媒体从业者&…

TiDB-从0到1-BR工具

TiDB从0到1系列 TiDB-从0到1-体系结构TiDB-从0到1-分布式存储TiDB-从0到1-分布式事务TiDB-从0到1-MVCCTiDB-从0到1-部署篇TiDB-从0到1-配置篇TiDB-从0到1-集群扩缩容 一、BR工具 BR工具全称backup & restore&#xff0c;如同MySQL可以通过mysqldump和xtrabackup进行备份…