标签:package 处理 read ant stat adl 允许 order name
1.读写锁:在同一时刻允许多个读线程访问,但是当写线程访问,所有的写线程和读线程均被阻塞。读写锁维护了一个读锁加一个写锁,通过读写锁分离的模式来保证线程安全,性能高于一般的排他锁。
读写锁分离设计模式的作用是在资源读写时,不用再去关心多线程所造成的读写冲突问题,读的时候加读锁,然后读完释放锁
同理,写的时候加写锁,写完释放锁。内在逻辑全部交给了ReadWriterLock来处理。
线程 | 读 | 写 |
---|---|---|
读 | 非阻塞(不冲突) | 阻塞(冲突) |
写 | 阻塞(冲突) | 阻塞(冲突) |
/** * @ClassName Entry * @Description TODO 实体类 * @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; } }
package com.mjtabu.api.lock; /** * 定义接口 */ public interface InterfaceClass { public Entry getEntry(); public void setEntry(String name); }
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * @ClassName RwLock * @Description TODO 接口实现类 > 读写分离Lock * @Date 2020/5/2 16:18 **/ 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(); } } }
/** * @ClassName TestRwLock * @Description TODO 测试 "读" "写" 锁 * @Date 2020/5/2 16:49 **/ 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(); } } } }
标签:package 处理 read ant stat adl 允许 order name
原文地址:https://www.cnblogs.com/mjtabu/p/12821222.html