java ReentrantLock 锁相关笔记

为什么80%的码农都做不了架构师?>>>   hot3.png

ReentrantLock重入锁简单理解就是对同一个线程而言,它可以重复的获取锁。例如这个线程可以连续获取两次锁,但是释放锁的次数也一定要是两次

Lock lock=new ReentrantLock(true);//公平锁
Lock lock=new ReentrantLock(false);//非公平锁

公平锁指的是线程获取锁的顺序是按照加锁顺序来的,而非公平锁指的是抢锁机制,先lock的线程不一定先获得锁。Java的synchronized关键字就是非公平锁

 

package com.example.web.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;@RestController
public class ThreadController {//测试每次请求是否重新初始化int single = 0;private ReentrantLock lock = new ReentrantLock();private ReentrantReadWriteLock lockRW = new ReentrantReadWriteLock();//设置程序进入等待状态private Condition condition = lock.newCondition();@RequestMapping("thread")public int test() {//练习锁机制,开启几个线程for (int i = 0; i < 5; i++) {new Thread(() -> {try {lock.lock();//此处测试线程等待和线程唤醒System.out.println("线程进入等待状态");condition.await();thread();} catch (Exception ex) {} finally {lock.unlock();}}).start();}single++;System.out.println(single);return single;}//写一个方法用来唤醒线程@RequestMapping("/thread/signal")public void signal() {lock.lock();condition.signal();lock.unlock();System.out.println("线程已被唤醒");}//写一个方法用来多线程调用public void thread() throws Exception {//写个循环用来多线程干扰for (int i = 0; i < 5; i++) {System.out.println("线程" + i + ":" + Thread.currentThread().getName());//得稍微耗时一下才行Thread.sleep(50);}}}

private ReentrantReadWriteLock lockRW = new ReentrantReadWriteLock();//读写锁

Lock类有读锁和写锁,读读共享,写写互斥,读写互斥

 

参考链接:https://www.cnblogs.com/-new/p/7256297.html

 

java原子类使用的就是原子锁,核心方法就是compareAndSet,也就是常说的CAS,用来对比更新,写一段伪代码,current会和实际值对比,如果相同则更新成next值,否则继续循环。

public final int incrementAndGet() {for (; ; ) {//获取当前值int current = get();//设置期望值int next = current + 1;//调用Native方法compareAndSet,执行CAS操作if (compareAndSet(current, next))//成功后才会返回期望值,否则无线循环return next;}
}

 

讲解自旋锁等相关知识:

https://blog.csdn.net/qq_34337272/article/details/81252853

https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247484911&amp;idx=1&amp;sn=1d53616437f50b353e33edad6fda2e4f&source=41#wechat_redirect

各种锁介绍:https://www.cnblogs.com/lzh-blogs/p/7477157.html

转载于:https://my.oschina.net/uwith/blog/3045525

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机启动程序bios_如何构建自己的计算机,第三部分:准备BIOS

计算机启动程序biosSo you’ve carefully picked out some parts and built a computer, but it doesn’t really do anything…yet. Before we hop into installing your operating system, we need to take a quick look at the BIOS and prepare it for our operating syste…

kindle图书免费下载_如何在Kindle上免费签出图书馆书籍

kindle图书免费下载Tired of paying so much for ebooks? Most libraries these days let you check out eBooks, for free, just like regular books. 厌倦了为电子书支付这么多钱&#xff1f; 如今&#xff0c;大多数图书馆都让您免费阅读电子书&#xff0c;就像普通书籍一样…

总结之:CentOS 6.4系统裁减详解及装载网卡步骤

前言 随着接触Linux的慢慢深入、对Linux也有了一个基本认识了吧&#xff0c;慢慢的接触系统内核、系统配置文件、在了解Linux的系统启动流程后&#xff0c;现在来总结一下一个简单的Linux系统的裁减方法和步骤&#xff0c;一个只有内核文件和几个简单的命令的小Linux系统&am…

android 设备占用_如何查看正在占用Android设备的空间

android 设备占用When you picked up your shiny new Android device, you probably thought “yeah, this has plenty of storage. I’ll never fill it up!” But here you are, some number of months later with a full phone and no clue why. No worries: here’s how yo…

mysql密码正确却提示错误, 不输入密码反而能登录

今天部署阿里云服务器, 发现之前可以连接的mysql服务器突然连接不上了, 密码我确认是正确的,但登录时就是显示密码错误, 很崩溃, 差点气得我就想重装mysql了。 好在经过几番苦寻找到了以下能解决我问题的资料&#xff0c; 成功解决了我的问题&#xff0c; 万分感谢&#xff0c;…

php旧版本windows_Windows的旧版本中如何进行多任务处理?

php旧版本windowsConsidering that DOS was a single-tasking OS and the ties it had with early versions of Windows, just how did earlier versions of Windows manage to accomplish multi-tasking? Today’s SuperUser Q&A post looks at the answers to this ques…

docker swarm的应用----docker集群的构建

一、docker安装 这里我们安装docker-ce 的18.03版本 yum -y remove docker 删除原有版本 #安装依赖包 [rootDocker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2 #添加docker的CE版本的yum源配置文件 [rootDocker ~]# curl https://download.docker…

微信小程序 fire_如何在Fire TV和Fire TV Stick上侧面加载应用程序

微信小程序 fireAmazon’s Fire TV and Fire TV stick technically runs Android…but you wouldn’t know it from looking. Amazon has a wall of content for its set-top box, and doesn’t want Google (with its own competing platform) to crash the party. But even t…

设备无法获得谷歌运行怎么办_因此,您刚刚获得了Google主页。 怎么办?

设备无法获得谷歌运行怎么办So you scored a Google Home for Christmas. That’s awesome because this is a killer little smart speaker that can do a lot of different things—in fact, it can be a little overwhelming. The good news is that we’ve got you covered…

IDEA Maven创建多个Module相互依赖

1、前言 在大型企业项目中&#xff0c;系统架构复杂多变&#xff0c;一个项目根本无法支撑起所有业务。为了提高项目扩展性、灵活性、重用性&#xff0c;封装性&#xff0c;将项目分为多个Module是非常必要的。 这里就不说IDEA如何安装了&#xff0c;安装好IDEA后需要修改maven…

速达5000出现计算成本数据溢出的问题

算成本提示某货品成本溢出处理方法&#xff0c;该问题是由于货品成本异常&#xff0c;成本上亿或者负亿造成的&#xff1b; 1.首先通过语句&#xff1a;select * into tmp_goods from l_goods where ABS(aprice)>100000&#xff0c;把成本价格超过10万的货品资料取出&#x…

收银员英文缩写_如何在没有收银员的苹果商店购买东西

收银员英文缩写If you visit an Apple Store in the hopes of buying a new iPhone, iPad, or MacBook, you have to talk to an Apple employee, since all the expensive products are kept in the back. However, if it’s just an accessory you want, you can buy it with…

php表单提交完返回,表单内容不清空解决方法

2019独角兽企业重金招聘Python工程师标准>>> 我们经常在注册的时候&#xff0c;填写一大推信息以后在提交注册的时候&#xff0c;因为某一项信息不正确&#xff0c;在返回的时候之前的填写的内容全部没有了&#xff0c;这样会导致用户丧失再次填写的信息&#xff0c…

word标尺灰色_如何在Microsoft Word中使用标尺

word标尺灰色Word’s rulers let you control the margins of your page and the indentation of paragraphs. They’re great for precisely lining up images, text, and other elements. If you’re printing a document, the rulers can help ensure that what you see on …

Matplotlib学习---用matplotlib画误差线(errorbar)

误差线用于显示数据的不确定程度&#xff0c;误差一般使用标准差&#xff08;Standard Deviation&#xff09;或标准误差&#xff08;Standard Error&#xff09;。 标准差&#xff08;SD&#xff09;&#xff1a;是方差的算术平方根。如果是总体标准差&#xff0c;那么用σ表示…

Android One和Android Go有什么区别?

In 2014, Google announced a lineup of low-cost, low-spec phones called Android One. In 2017, they announced Android Go, specifically designed for low-cost, low-spec phones. So…what’s the difference? 2014年&#xff0c;Google宣布了一系列名为Android One的低…

outlook advanced find 快捷键不起作用

症状&#xff1a;用户反应按outlook advanced find的快捷键时无效&#xff0c;快捷键为CtrlShiftF。第一感觉是肯定跟别的软件有冲突了&#xff0c;观察了下&#xff0c;发现用户正在使用sougou拼音输入法&#xff0c;于是点其属性查看&#xff0c;果然发现与其的简繁切换冲突了…

21-while里的break简单用法

break是结束循环&#xff0c;break之后、循环体内代码不再执行。 while True:yn input(Continue(y/n): )if yn in [n,N]:breakprint(running......) 结果输出&#xff1a; 转载于:https://www.cnblogs.com/hejianping/p/10861816.html

视频造假_如何发现“深造假”面部切换视频

视频造假Recently, Reddit has been making news again with a subreddit in w hich people use a machine learning tool called “Deep Fake” to automatically replace one person’s face with another in a video. Obviously, since this is the internet, people are us…

找到特定ip地址 修改ip_您如何找到网站的IP地址?

找到特定ip地址 修改ipWhether you are in it just for a bit of geeky fun, or are seriously wanting to know the answer, how do you find out the IP address for a website? Today’s SuperUser Q&A post looks at the answer, and how to know if more than one we…