显示锁Lock
显示锁的应用场景
1.ReadWriteLock 读写分离:可以有效地减少锁竞争,以提升系统性能
2.中断锁,超时锁
Lock读写分离
读写分离简介:
读写锁:在同一时刻允许多个读线程访问,但是当写线程访问,所有的写线程和读线程均被阻塞。读写锁维护了一个读锁加一个写锁,通过读写锁分离的模式来保证线程安全,性能高于一般的排他锁。
读写锁分离设计模式的作用是在资源读写时,不用再去关心多线程所造成的读写冲突问题,读的时候加读锁,然后读完释放锁
同理,写的时候加写锁,写完释放锁。内在逻辑全部交给了ReadWriterLock来处理。
遵循原则
1.read read 并行化
2.read write 不允许
3.write write 不允许
读写锁的访问约束情况
线程 读 写 读 非阻塞(不冲突) 阻塞(冲突) 写 阻塞(冲突) 阻塞(冲突)
demo实现逻辑
1.创建实体类
/**
* @Author 踏步
* @Description 实体类
* @Date 2020/5/2 16:30
**/
public class Entry {
private String name;
public Entry(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.定义接口
/**
* @Author 踏步
* @Description 定义接口
* @Date 2020/5/2 16:30
**/
public interface InterfaceClass {
public Entry getEntry();
public void setEntry(String name);
}
3.创建接口的实现类
4.在接口实现类中模拟 "读" "写" 锁 的实现
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* @Author 踏步
* @Description 接口实现类 > 读写分离Lock
* @Date 2020/5/2 16:30
**/
public class RwLock implements InterfaceClass {
// 实体类
private Entry entry;
// 创建读写锁
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock readLock = lock.readLock();
private final Lock writeLock = lock.writeLock();
// 构造方法
public RwLock(Entry entry){
this.entry = entry;
}
/**
* 读锁
* @return
*/
@Override
public Entry getEntry() {
readLock.lock();
try {
//System.out.println("读操作逻辑处理");
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readLock.unlock();
}
return entry;
}
/**
* 写锁
* @param name
*/
@Override
public void setEntry(String name) {
writeLock.lock();
try {
//System.out.println("写操作逻辑处理");
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writeLock.unlock();
}
}
}
5.创建测试类 > 在测试类中创建线程并测试 "读" "写" 锁
/**
* @Author 踏步
* @Description 测试 "读" "写" 锁
* @Date 2020/5/2 16:30
**/
public class TestRwLock {
static final int threadNum = 3;
static final int readThreadNum = 10;
/**
* 读线程
*/
private static class ReadThread implements Runnable{
private InterfaceClass interfaceClass;
private ReadThread(InterfaceClass interfaceClass){
this.interfaceClass = interfaceClass;
}
@Override
public void run() {
// 开始时间
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
// 读操作方法
interfaceClass.getEntry();
}
System.out.println(Thread.currentThread().getName()+"读操作功能耗时:"+(System.currentTimeMillis()-start)+"ms");
}
}
/**
* 写线程
*/
private static class WriteThread extends Thread{
private InterfaceClass interfaceClass;
private WriteThread(InterfaceClass interfaceClass){
this.interfaceClass = interfaceClass;
}
@Override
public void run() {
// 开始时间
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
// 写操作方法
interfaceClass.setEntry("皮卡兵");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"写操作功能耗时:"+(System.currentTimeMillis()-start)+"ms");
}
}
public static void main(String[] args) {
Entry entry = new Entry("皮卡丘");
InterfaceClass interfaceClass = new RwLock(entry);
for (int i = 0; i < threadNum; i++) {
Thread writeThread = new WriteThread(interfaceClass);
writeThread.start();
for (int j = 0; j < readThreadNum; j++) {
Thread readThread = new Thread(new ReadThread(interfaceClass));
readThread.run();
}
}
}
}