第33篇:怎么演示公平锁和非公平锁

star2017 1年前 ⋅ 485 阅读

环境:jdk1.8。

今天群里面刚有有人在问这块的东西,那就拿出来说一下。

本文主要用juc中的ReentrantLock来说一下公平锁和非公平锁的东西。

先理解一下什么是公平锁、非公平锁?

公平锁和非公平锁体现在别人释放锁的一瞬间,如果前面已经有排队的,新来的是否可以插队,如果可以插队表示是非公平的,如果不可用插队,只能排在最后面,是公平的方式。

示例

测试公平锁和非公平锁的时候,可以这么来:在主线程中先启动一个t1线程,在t1里面获取锁,获取锁之后休眠一会,然后在主线中启动10个father线程去排队获取锁,然后在t1中释放锁代码的前面一步再启动一个线程,在这个线程内部再创建10个son线程,去获取锁,看看后面这10个son线程会不会排到上面10个father线程前面去,如果会表示插队了,说明是非公平的,如果不会,表示排队执行的,说明是公平的方式,示例代码如下:

  1. package com.itsoku.chat32;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.concurrent.locks.ReentrantLock;
  5. /**
  6. * 跟着阿里p7学并发,微信公众号:javacode2018
  7. */
  8. @Slf4j
  9. public class Demo8 {
  10. public static void main(String[] args) throws InterruptedException {
  11. //非公平锁
  12. test1(false);
  13. TimeUnit.SECONDS.sleep(4);
  14. log.info("------------------------------");
  15. //公平锁
  16. test1(true);
  17. }
  18. public static void test1(boolean fair) throws InterruptedException {
  19. ReentrantLock lock = new ReentrantLock(fair);
  20. Thread t1 = new Thread(() -> {
  21. lock.lock();
  22. try {
  23. log.info("start");
  24. TimeUnit.SECONDS.sleep(3);
  25. new Thread(() -> {
  26. m1(lock, "son");
  27. }).start();
  28. log.info("end");
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. } finally {
  32. lock.unlock();
  33. }
  34. });
  35. t1.setName("t1");
  36. t1.start();
  37. TimeUnit.SECONDS.sleep(1);
  38. m1(lock, "father");
  39. }
  40. public static void m1(ReentrantLock lock, String threadPre) {
  41. for (int i = 0; i < 10; i++) {
  42. Thread thread = new Thread(() -> {
  43. lock.lock();
  44. try {
  45. log.info("获取到锁!");
  46. } finally {
  47. lock.unlock();
  48. }
  49. });
  50. thread.setName(threadPre + "-" + i);
  51. thread.start();
  52. }
  53. }
  54. }

输出:

  1. 10:16:02.132 [t1] INFO com.itsoku.chat32.Demo8 - start
  2. 10:16:05.135 [t1] INFO com.itsoku.chat32.Demo8 - end
  3. 10:16:05.135 [father-0] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  4. 10:16:05.136 [father-1] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  5. 10:16:05.136 [father-2] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  6. 10:16:05.136 [son-2] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  7. 10:16:05.136 [father-3] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  8. 10:16:05.137 [father-4] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  9. 10:16:05.137 [father-5] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  10. 10:16:05.137 [son-5] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  11. 10:16:05.137 [father-6] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  12. 10:16:05.137 [father-7] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  13. 10:16:05.137 [father-8] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  14. 10:16:05.138 [father-9] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  15. 10:16:05.138 [son-0] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  16. 10:16:05.138 [son-1] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  17. 10:16:05.138 [son-3] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  18. 10:16:05.138 [son-4] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  19. 10:16:05.138 [son-6] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  20. 10:16:05.138 [son-7] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  21. 10:16:05.139 [son-8] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  22. 10:16:05.139 [son-9] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  23. 10:16:07.129 [main] INFO com.itsoku.chat32.Demo8 - ------------------------------
  24. 10:16:07.129 [t1] INFO com.itsoku.chat32.Demo8 - start
  25. 10:16:10.130 [t1] INFO com.itsoku.chat32.Demo8 - end
  26. 10:16:10.130 [father-0] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  27. 10:16:10.130 [father-1] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  28. 10:16:10.130 [father-2] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  29. 10:16:10.131 [father-3] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  30. 10:16:10.131 [father-4] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  31. 10:16:10.131 [father-5] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  32. 10:16:10.131 [father-6] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  33. 10:16:10.132 [father-7] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  34. 10:16:10.132 [father-8] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  35. 10:16:10.132 [father-9] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  36. 10:16:10.132 [son-1] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  37. 10:16:10.132 [son-0] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  38. 10:16:10.132 [son-2] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  39. 10:16:10.133 [son-4] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  40. 10:16:10.133 [son-3] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  41. 10:16:10.133 [son-5] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  42. 10:16:10.133 [son-6] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  43. 10:16:10.133 [son-7] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  44. 10:16:10.133 [son-8] INFO com.itsoku.chat32.Demo8 - 获取到锁!
  45. 10:16:10.135 [son-9] INFO com.itsoku.chat32.Demo8 - 获取到锁!

运行代码可以创建一个springboot项目,需要安装lombook插件

上面代码中以son开头的线程在father线程之后启动的,分析一下结果:

test1(false);执行的是非公平锁的过程,看一下son的输出排到father前面去了,说明插队了,说明采用的是非公平锁的方式。

test1(true);执行的是公平锁的过程,看一下输出,son都是在father后面输出的,说明排队执行的,说明采用的是公平锁的方式。

最新资料

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: