可重入锁🔒的一点小事儿
2018-05-21 本文已影响0人
happyleijun
对于java的可重入锁借助下面这个例子进行一点说明
问题:启动两个线程,线程1依次打印1,2,3;线程2再依次打印4,5,6;然后线程1接着打印7,8,9
package cn.focus.adv.hero.util;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Copyright (C) 1998 - 2016 SOHU Inc., All Rights Reserved.
* <p>
*
* @Author: hanleijun (leijunhan@sohu-inc.com)
* @Date: 2018/5/21
*/
public class TestLocl {
public static void main(String[] args) {
final AtomicInteger value = new AtomicInteger(1);
Lock lock = new ReentrantLock();
Condition cThree = lock.newCondition();
Condition cSix = lock.newCondition();
Thread thread1 = new Thread(() -> {
lock.lock();
System.out.println("A is coming");
while(value.get() <= 3){
System.out.println(Thread.currentThread().getName() + " is printing: " + value.get());
value.incrementAndGet();
}
cThree.signal();
lock.unlock();
lock.lock();
try {
System.out.println("A is coming again");
cSix.await();
while(value.get() <=9){
System.out.println(Thread.currentThread().getName() + " is printing: " + value.get());
value.incrementAndGet();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
});
Thread thread2 = new Thread(() -> {
lock.lock();
System.out.println("2---1");
try {
// while(value.get() <= 3){ // **
cThree.await();
// }
System.out.println("B is coming");
while(value.get() <= 6){
System.out.println(Thread.currentThread().getName() + " is printing: " + value.get());
value.incrementAndGet();
}
cSix.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
});
thread1.start();
thread2.start();
}
}
· 在线程执行体内的外部变量需要为final的,所以不可以用int或integer,这里用了AtomicInteger这一封装类
· 代码中注释掉掉部分,如果不打开,则程序可能卡死,原因是,如果线程2先于线程1启动,则先await,之后按照正常逻辑,不会有问题,如果是线程1先于线程2启动,则打印1,2,3之后signal了第一个信号量,然后线程2await这个信号量则永远不能得到(先signal后await为非法流程),所以线程2在await之前需要加入条件判断
· 之所以用while而不是if,原因是:为了防止虚假唤醒,当然如果改成if也是可以正常运行的,虚假唤醒指的是await的线程,没有收到针对于自己的notify就被唤醒了,一般处理方式就是在await前加入条件判断,使用while()