在Java多线程中,我们知道可以使用synchronized关键字来实现线程间的同步互斥工作,那么其实还有一个更优秀的机制去完成这个“同步互斥”工作,它就是Lock对象,我们主要学习两种锁,重入锁和读写锁。它们具有比synchronized更为强大的功能,并且有嗅探锁定、多路分支等功能。
重入锁(ReentrantLock)是一个java.util.concurrent.locks包下实现了Lock接口的类,如下所示:
- public class ReentrantLock implements Lock{
- ...
- }
重入锁在需要进行同步的代码部分加上锁定,但不要忘记最后一定要释放锁定,不然会造成锁永远无法释放,其它线程永远进不来的结果。
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class UseReentrantLock {
-
- private Lock lock = new ReentrantLock();
-
- public void method1() {
- lock.lock();
- try {
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");
- Thread.sleep(1000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public void method2() {
- lock.lock();
- try {
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");
- Thread.sleep(2000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public static void main(String[] args) {
- final UseReentrantLock ur = new UseReentrantLock();
-
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- ur.method1();
- }
- }, "t1");
-
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- ur.method2();
- }
- }, "t2");
-
- t1.start();
- t2.start();
- }
- }
执行上述代码,其输出结果为:
- 当前线程:t1进入method1..
- 当前线程:t1退出method1..
- 当前线程:t2进入method2..
- 当前线程:t2退出method2..
线程t2在执行method2的时候由于线程t1释放锁了,所以线程t2可以获取到锁,method2方法正常运行。
像下面这种情况由于线程t1未释放锁,线程t2一直处于阻塞状态:
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class UseReentrantLock {
-
- private Lock lock = new ReentrantLock();
-
- public void method1() {
- lock.lock();
- try {
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");
- Thread.sleep(1000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- //lock.unlock();
- }
- }
-
- public void method2() {
- lock.lock();
- try {
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");
- Thread.sleep(2000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public static void main(String[] args) {
- final UseReentrantLock ur = new UseReentrantLock();
-
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- ur.method1();
- }
- }, "t1");
-
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- ur.method2();
- }
- }, "t2");
-
- t1.start();
- t2.start();
- }
- }
执行上述代码,可以看到仅仅线程t1的执行结果进行了打印,线程t2由于未获得锁一直处于阻塞状态
- 当前线程:t1进入method1..
- 当前线程:t1退出method1..
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class UseReentrantLock {
-
- private Lock lock = new ReentrantLock();
-
- public void method1() {
- lock.lock();
- try {
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");
- Thread.sleep(1000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
-
- }
- }
-
- public void method2() {
- lock.lock();
- try {
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");
- Thread.sleep(2000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public static void main(String[] args) {
- final UseReentrantLock ur = new UseReentrantLock();
-
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- ur.method1();
- ur.method2();
- }
- }, "t1");
-
- t1.start();
- }
- }
执行上述代码,其输出结果为:
- 当前线程:t1进入method1..
- 当前线程:t1退出method1..
- 当前线程:t1进入method2..
- 当前线程:t1退出method2..
可以看到线程t1在执行method1方法时虽然未释放锁,但是线程t1在执行method2时需要获得锁任然可以再次获得锁,即实现了锁重入的功能。
还记得我们在使用synchronized的时候,如果需要多线程间进行协作工作则需要Object的wait()和notify()、notifyAlI()方法进行配合工作。那么同样,我们在使用Lock的时候,可以使用一个新的等待/通知的类,它就是Condition。这个Condition一定是针对具体某一把锁的。也就是在只有锁的基础之上才会产生Condition。
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class UseCondition {
-
- private Lock lock = new ReentrantLock();
- private Condition condition = lock.newCondition();
-
- public void method1() {
- try {
- lock.lock();
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");
- Thread.sleep(3000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "释放锁..");
- condition.await(); // Object wait
- System.out.println("当前线程:" + Thread.currentThread().getName() + "继续执行...");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public void method2() {
- try {
- lock.lock();
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");
- Thread.sleep(3000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒..");
- condition.signal(); //Object notify
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public static void main(String[] args) {
-
- final UseCondition uc = new UseCondition();
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- uc.method1();
- }
- }, "t1");
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- uc.method2();
- }
- }, "t2");
-
- t1.start();
- t2.start();
- }
- }
执行上述代码,其输出结果:
- 当前线程:t1进入..
- 当前线程:t1释放锁..
- 当前线程:t2进入..
- 当前线程:t2发出唤醒..
- 当前线程:t1继续执行...
此处需要特别注意的是t2线程的唤醒操作是不会释放锁的,必须在调用了lock.unlock()方法以后才能释放锁。
我们可以通过一个Lock对象产生多个Condition进行多线程间的交互,非常的灵活。可以使得部分需要唤醒的线程唤醒,其他线程则继续等待通知。
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class UseManyCondition {
-
- private ReentrantLock lock = new ReentrantLock();
- private Condition c1 = lock.newCondition();
- private Condition c2 = lock.newCondition();
-
- public void m1(){
- try {
- lock.lock();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");
- c1.await();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续..");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public void m2(){
- try {
- lock.lock();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待..");
- c1.await();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续..");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public void m3(){
- try {
- lock.lock();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待..");
- c2.await();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续..");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public void m4(){
- try {
- lock.lock();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
- c1.signalAll();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public void m5(){
- try {
- lock.lock();
- System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
- c2.signal();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
-
- public static void main(String[] args) {
- final UseManyCondition umc = new UseManyCondition();
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- umc.m1();
- }
- },"t1");
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- umc.m2();
- }
- },"t2");
- Thread t3 = new Thread(new Runnable() {
- @Override
- public void run() {
- umc.m3();
- }
- },"t3");
- Thread t4 = new Thread(new Runnable() {
- @Override
- public void run() {
- umc.m4();
- }
- },"t4");
- Thread t5 = new Thread(new Runnable() {
- @Override
- public void run() {
- umc.m5();
- }
- },"t5");
-
- t1.start(); // c1
- t2.start(); // c1
- t3.start(); // c2
-
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- t4.start(); // c1
-
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- t5.start(); // c2
- }
- }
执行上述代码,其输出结果:
- 当前线程:t1进入方法m1等待..
- 当前线程:t3进入方法m3等待..
- 当前线程:t2进入方法m2等待..
- 当前线程:t4唤醒..
- 当前线程:t1方法m1继续..
- 当前线程:t2方法m2继续..
- 当前线程:t5唤醒..
- 当前线程:t3方法m3继续..
c1.signalAll()表示唤醒所有线程,c2.signal()表示随机唤醒一个线程
公平锁与非公平锁:
Lock lock = new ReentrantLock(boolean isFair);
Lock用法:
- tryLock():尝试获得锁,根据嗅探获得结果用true/false返回,不会产生锁等待
- tryLock(long timeout, TimeUnit unit):在给定的时间内尝试获得锁,获得结果用truelfalse返回
- isFair():是否是公平锁
- isLocked():是否锁定
- getHoldCount():查询当前线程保持此锁的个数,也就是调用lock()次数
- lockInterruptibly():优先响应中断的锁
- getQueueLength():返回正在等待获取此锁定的线程数
- getWaitQueueLength():返回等待与锁定相关的给定条件Condition的线程数
- hasQueuedThread(Thread thread):查询指定的线程是否正在等待此锁
- hasQueuedThreads():查询是否有线程正在等待此锁
- hasWaiters():查询是否有线程正在等待与此锁定有关的condition条件
ReadWriteLock是 java.util.concurrent.locks 包下的一个接口,ReentrantReadWriteLock是java.util.concurrent.locks 包下的一个类,ReentrantReadWriteLock(读写锁)是ReadWriteLock的一个实现类。
- package java.util.concurrent.locks;
-
- public interface ReadWriteLock {
- ...
- }
- package java.util.concurrent.locks;
-
- public class ReentrantReadWriteLock implements ReadWriteLock{
- ...
- }
读写锁ReentrantReadWriteLock,其核心就是实现读写分离的锁。在高并发访问下,尤其是读多写少的情况下,性能要远高于重入锁。
之前学synchronized、ReentrantLock时,我们知道,同一时间内,只能有一个线程进行访问被锁定的代码,那么读写锁则不同,其本质是分成两个锁,即读锁、写锁。在读锁下,多个线程可以并发的进行访问,但是在写锁的时候,只能一个一个的顺序访问。
口诀:读读共享,写写互斥、读写互斥
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
-
- public class UseReentrantReadWriteLock {
-
- private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
- private ReadLock readLock = rwLock.readLock();
-
- public void read(){
- try {
- readLock.lock();
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
- Thread.sleep(3000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- readLock.unlock();
- }
- }
-
- public static void main(String[] args) {
-
- final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();
-
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- urrw.read();
- }
- }, "t1");
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- urrw.read();
- }
- }, "t2");
-
- //多个读操作的线程可以并发的去执行
- t1.start();
- t2.start();
- }
- }
执行上述代码,其输出内容为:
- 当前线程:t2进入...
- 当前线程:t1进入...
- 当前线程:t1退出...
- 当前线程:t2退出...
如果是进行读操作的话,多个线程可以并发的访问
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
-
- public class UseReentrantReadWriteLock {
-
- private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
- private ReadLock readLock = rwLock.readLock();
- private WriteLock writeLock = rwLock.writeLock();
-
- public void read(){
- try {
- readLock.lock();
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
- Thread.sleep(3000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- readLock.unlock();
- }
- }
-
- public void write(){
- try {
- writeLock.lock();
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
- Thread.sleep(3000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- writeLock.unlock();
- }
- }
-
- public static void main(String[] args) {
-
- final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();
-
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- urrw.read();
- }
- }, "t1");
- Thread t3 = new Thread(new Runnable() {
- @Override
- public void run() {
- urrw.write();
- }
- }, "t3");
-
- //读写互斥
- t1.start();
- t3.start();
- }
- }
执行上述代码,其输出结果为:
- 当前线程:t1进入...
- 当前线程:t1退出...
- 当前线程:t3进入...
- 当前线程:t3退出...
可以看到只有当线程t1的读锁释放以后,线程t2才能获取到写锁
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
-
- public class UseReentrantReadWriteLock {
-
- private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
- private WriteLock writeLock = rwLock.writeLock();
-
- public void write(){
- try {
- writeLock.lock();
- System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
- Thread.sleep(3000);
- System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- writeLock.unlock();
- }
- }
-
- public static void main(String[] args) {
-
- final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();
-
- Thread t3 = new Thread(new Runnable() {
- @Override
- public void run() {
- urrw.write();
- }
- }, "t3");
-
- Thread t4 = new Thread(new Runnable() {
- @Override
- public void run() {
- urrw.write();
- }
- }, "t4");
-
- // 写写互斥
- t3.start();
- t4.start();
- }
- }
执行上述代码,其输出结果为:
- 当前线程:t3进入...
- 当前线程:t3退出...
- 当前线程:t4进入...
- 当前线程:t4退出...
可以看到只有当线程t3的读锁释放以后,线程t4才能获取到读锁
1、避免死锁
2、减小锁的持有时间
3、减小锁的粒度
4、锁的分离
5、尽量使用无锁的操作,如原子操作(Atomic系列类),锁升级优化