目录:
1. ReentrantLock(重入锁)
2. 重入锁,使用Condition类,实现锁于等待/通知
3. ReentrantReadWriteLock(读写锁)
在java多线程种,我们知道可以使用synchronized关键字来实现线程间的同步互斥工作,那么其实还有一个更优秀的机制完成这个“同步互斥”工作,他就是Lock对象,它主要有两种锁,重入锁和读写锁。她们具有比synchronized更为强大的功能,并且有嗅探锁定、多路分支等功能。
1、ReentrantLock(重入锁)
重入锁,在需要进行同步的代码部分加上锁,但不要忘记最后一定要释放锁定,不然会造成锁永远无法释放,其他线程永远进不来的结果。官方推荐的做法就是在finally里面释放锁。
class X {
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}
测试代码:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class UseReentrantLock {
private Lock lock = new ReentrantLock();
public void method1(){
try {
//获取锁
lock.lock();
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");
Thread.sleep(1000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//释放锁,如果不释放锁T1或者T2会有其中一个线程进不来
lock.unlock();
}
}
public void method2(){
try {
//获取锁
lock.lock();
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");
Thread.sleep(2000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//释放锁,如果不释放锁T1或者T2会有其中一个线程进不来
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");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
ur.method1();
ur.method2();
}
}, "t2");
//T1线程启动
t1.start();
t2.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2、重入锁,使用Condition类,实现锁于等待/通知
在使用synchronized的时候,如果需要多线程间进行协作工作,则需要Object的wait()和notify()、notifyAll()方法进行配合工作。
那么同样,我们在使用Lock的时候,可以使用一个新的等待/通知的类,它就和Condition.这个Condition一定是针对具体某一把锁的。也就是在只有锁的基础之上才会产生Condition。
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() + "释放锁..");
//使当前线程等待它暗示或 interrupted,等待被其他线程唤醒
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();
}
Condition还有个一个比Object wait/Object notify更强大的地方就是它支持一个Lock对象产生多个Condition进行多线程间
的交互,非常的灵活。可以使用部分需要唤醒的线程唤醒,其他线程则继续等待通知。实现这个功能主要是靠signalAll()
唤醒所有等待线程。
测试示例
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class UseManyCondition {
private ReentrantLock lock = new ReentrantLock();
//一个lock产生多个Condition对象
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
public void m1(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");
//使当前线程等待它暗示或 interrupted,等待被其他线程唤醒
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等待..");
//使当前线程等待它暗示或 interrupted,等待被其他线程唤醒
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等待..");
//使当前线程等待它暗示或 interrupted,等待被其他线程唤醒
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(() -> umc.m1(),"t1");
Thread t2 = new Thread(() -> umc.m2(),"t2");
Thread t3 = new Thread(() -> umc.m3(),"t3");
Thread t4 = new Thread(() -> umc.m4(),"t4");
Thread t5 = new Thread(() -> 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
}
}
3、ReentrantReadWriteLock(读写锁)
读写锁ReenTrantReadWriteLock,其核心就是实现读写分离的锁。在高并发访问下,尤其是读多写少的情况下,性能要远高于重入锁。
synchronized、ReentrantLock同一时间内,它们只能支持一个线程进行访问被锁定的代码。读写锁则不同,其本质是分成两个锁,即读锁、写锁。在读锁下,多个线程可以并发的进行访问,但是在写锁的时候,只能一个一个的顺序访问。
写入锁提供了一个Condition实施的行为,以同样的方式,相对于写锁,由ReentrantLock.newCondition()的Condition实现对ReentrantLock。当然这Condition可以,只能用写锁。
读锁不支持Condition和readLock().newCondition()抛出UnsupportedOperationException。
口诀:读读共享,写写互斥,读写互斥。
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(() -> urrw.read(), "t1");
Thread t2 = new Thread(() -> urrw.read(), "t2");
Thread t3 = new Thread(() -> urrw.write(), "t3");
Thread t4 = new Thread(() -> urrw.write(), "t4");
//读读共享
t1.start();
t2.start();
//读写互斥
// t1.start(); // R
// t3.start(); // W
//写写互斥
// t3.start();
// t4.start();
}
}