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

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

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

示例

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

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/
微信公众号:程序员路人,个人博客:http://www.itsoku.com
/
@Slf4j
public class Demo8 {
public static void main(String[] args) throws InterruptedException {
//非公平锁
test1(
false);
TimeUnit.SECONDS.sleep(4);
log.info(“———————————————“);
//公平锁
test1(
true**);
}

  1. **public** **static** **void** **test1**(**boolean** fair) **throws** InterruptedException {<br /> ReentrantLock lock = **new** ReentrantLock(fair);<br /> Thread t1 = **new** Thread(() -> {<br /> lock.lock();<br /> **try** {<br /> log.info("start");<br /> TimeUnit.SECONDS.sleep(3);<br /> **new** Thread(() -> {<br /> m1(lock, "son");<br /> }).start();<br /> log.info("end");<br /> } **catch** (InterruptedException e) {<br /> e.printStackTrace();<br /> } **finally** {<br /> lock.unlock();<br /> }<br /> });<br /> t1.setName("t1");<br /> t1.start();<br /> TimeUnit.SECONDS.sleep(1);<br /> m1(lock, "father");<br /> }
  2. **public** **static** **void** **m1**(ReentrantLock lock, String threadPre) {<br /> **for** (**int** i = 0; i < 10; i++) {<br /> Thread thread = **new** Thread(() -> {<br /> lock.lock();<br /> **try** {<br /> log.info("获取到锁!");<br /> } **finally** {<br /> lock.unlock();<br /> }<br /> });<br /> thread.setName(threadPre + "-" + i);<br /> thread.start();<br /> }<br /> }<br />}

输出:
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后面输出的,说明排队执行的,说明采用的是公平锁的方式。