Java充电社
专辑
博文
联系我
本人继续续收门徒,亲手指导
第33篇:怎么演示公平锁和非公平锁
相关专辑:
Java高并发教程
<div style="display:none"></div> 环境:jdk1.8。 今天群里面刚有有人在问这块的东西,那就拿出来说一下。 本文主要用juc中的`ReentrantLock`来说一下公平锁和非公平锁的东西。 ## 先理解一下什么是公平锁、非公平锁? 公平锁和非公平锁体现在别人释放锁的一瞬间,如果前面已经有排队的,新来的是否可以插队,如果可以插队表示是非公平的,如果不可用插队,只能排在最后面,是公平的方式。 ## 示例 > 测试公平锁和非公平锁的时候,可以这么来:在主线程中先启动一个t1线程,在t1里面获取锁,获取锁之后休眠一会,然后在主线中启动10个father线程去排队获取锁,然后在t1中释放锁代码的前面一步再启动一个线程,在这个线程内部再创建10个son线程,去获取锁,看看后面这10个son线程会不会排到上面10个father线程前面去,如果会表示插队了,说明是非公平的,如果不会,表示排队执行的,说明是公平的方式,示例代码如下: ```java package com.itsoku.chat32; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; /** * 跟着阿里p7学并发,微信公众号:javacode2018 */ @Slf4j public class Demo8 { public static void main(String[] args) throws InterruptedException { //非公平锁 test1(false); TimeUnit.SECONDS.sleep(4); log.info("------------------------------"); //公平锁 test1(true); } public static void test1(boolean fair) throws InterruptedException { ReentrantLock lock = new ReentrantLock(fair); Thread t1 = new Thread(() -> { lock.lock(); try { log.info("start"); TimeUnit.SECONDS.sleep(3); new Thread(() -> { m1(lock, "son"); }).start(); log.info("end"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }); t1.setName("t1"); t1.start(); TimeUnit.SECONDS.sleep(1); m1(lock, "father"); } public static void m1(ReentrantLock lock, String threadPre) { for (int i = 0; i < 10; i++) { Thread thread = new Thread(() -> { lock.lock(); try { log.info("获取到锁!"); } finally { lock.unlock(); } }); thread.setName(threadPre + "-" + i); thread.start(); } } } ``` 输出: ``` 10:16:02.132 [t1] INFO com.itsoku.chat32.Demo8 - start 10:16:05.135 [t1] INFO com.itsoku.chat32.Demo8 - end 10:16:05.135 [father-0] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.136 [father-1] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.136 [father-2] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.136 [son-2] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.136 [father-3] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.137 [father-4] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.137 [father-5] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.137 [son-5] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.137 [father-6] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.137 [father-7] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.137 [father-8] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [father-9] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [son-0] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [son-1] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [son-3] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [son-4] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [son-6] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.138 [son-7] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.139 [son-8] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:05.139 [son-9] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:07.129 [main] INFO com.itsoku.chat32.Demo8 - ------------------------------ 10:16:07.129 [t1] INFO com.itsoku.chat32.Demo8 - start 10:16:10.130 [t1] INFO com.itsoku.chat32.Demo8 - end 10:16:10.130 [father-0] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.130 [father-1] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.130 [father-2] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.131 [father-3] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.131 [father-4] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.131 [father-5] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.131 [father-6] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.132 [father-7] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.132 [father-8] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.132 [father-9] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.132 [son-1] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.132 [son-0] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.132 [son-2] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.133 [son-4] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.133 [son-3] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.133 [son-5] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.133 [son-6] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.133 [son-7] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.133 [son-8] INFO com.itsoku.chat32.Demo8 - 获取到锁! 10:16:10.135 [son-9] INFO com.itsoku.chat32.Demo8 - 获取到锁! ``` > 运行代码可以创建一个springboot项目,需要安装lombook插件 上面代码中以`son`开头的线程在`father`线程之后启动的,分析一下结果: `test1(false);`执行的是非公平锁的过程,看一下`son`的输出排到`father`前面去了,说明插队了,说明采用的是非公平锁的方式。 `test1(true);`执行的是公平锁的过程,看一下输出,`son`都是在`father`后面输出的,说明排队执行的,说明采用的是公平锁的方式。 <a style="display:none" target="_blank" href="https://mp.weixin.qq.com/s/_S1DD2JADnXvpexxaBwLLg" style="color:red; font-size:20px; font-weight:bold">继续收门徒,亲手带,月薪 4W 以下的可以来找我</a> ## 最新资料 1. <a href="https://mp.weixin.qq.com/s?__biz=MzkzOTI3Nzc0Mg==&mid=2247484964&idx=2&sn=c81bce2f26015ee0f9632ddc6c67df03&scene=21#wechat_redirect" target="_blank">尚硅谷 Java 学科全套教程(总 207.77GB)</a> 2. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484192&idx=1&sn=505f2faaa4cc911f553850667749bcbb&scene=21#wechat_redirect" target="_blank">2021 最新版 Java 微服务学习线路图 + 视频</a> 3. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484573&idx=1&sn=7f3d83892186c16c57bc0b99f03f1ffd&scene=21#wechat_redirect" target="_blank">阿里技术大佬整理的《Spring 学习笔记.pdf》</a> 4. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484544&idx=2&sn=c1dfe907cfaa5b9ae8e66fc247ccbe84&scene=21#wechat_redirect" target="_blank">阿里大佬的《MySQL 学习笔记高清.pdf》</a> 5. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485167&idx=1&sn=48d75c8e93e748235a3547f34921dfb7&scene=21#wechat_redirect" target="_blank">2021 版 java 高并发常见面试题汇总.pdf</a> 6. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485664&idx=1&sn=435f9f515a8f881642820d7790ad20ce&scene=21#wechat_redirect" target="_blank">Idea 快捷键大全.pdf</a> ![](https://itsoku.oss-cn-hangzhou.aliyuncs.com/itsoku/blog/article/1/2883e86e-3eff-404a-8943-0066e5e2b454.png)
相关专辑:
Java高并发教程