Read-Write Lock
Article directory
Read write lock mode introduction
In read write lock mode, read and write operations are considered separately. When performing write operations, the thread must acquire the lock for writing; when performing read operations, the thread needs to acquire the lock for reading;
Read write lock usage scenario
- In the multithreaded environment, the same object can be accessed, some of which will change the state of the object, while others won't;
- It is suitable for scenarios where the read operation frequency is higher than the write operation frequency;
- When the read operation is heavy, if single thread execution is limited, it will have a great impact on the system;
Read write lock sample code analysis
//Multithreaded access to secure data classes public class Data { private final char[] buffer; private final ReadWriteLock lock=new ReadWriteLock(); public Data(int size){ this.buffer=new char[size]; for(int i=0;i<size;i++){ buffer[i]='*'; } } public char[] read()throws InterruptedException{ lock.readLock(); try{ return doRead(); }finally{ lock.readUnlock(); } } public void write(char c)throws InterruptedException{ lock.writeLock(); try{ doWrite(c); }finally{ lock.writeUnlock(); } } private char[] doRead(){ char[] newBuf=new char[buffer.length]; for(int i=0;i<buffer.length;i++){ newBuf[i]=buffer[i]; } slowly(); return newBuf; } private void doWrite(char c){ for(int i=0;i<buffer.length;i++){ buffer[i]=c; slowly(); } } private void slowly(){ try{ Thread.sleep(50); }catch(InterruptedException e){ e.printStackTrace(); } } } //Read write lock public class ReadWriteLock { private int readingReaders=0; private int waitingWriters=0; private int writingWriters=0; private boolean preferWriter=true; public synchronized void readLock() throws InterruptedException{ while(writingWriters>0||(preferWriter&&waitingWriters>0)){ wait(); } readingReaders++; } public synchronized void readUnlock(){ readingReaders--; preferWriter=true; notifyAll(); } public synchronized void writeLock()throws InterruptedException{ waitingWriters++; try{ while(readingReaders>0||writingWriters>0){ wait(); } }finally{ waitingWriters--; } writingWriters++; } public synchronized void writeUnlock(){ writingWriters--; preferWriter=false; notifyAll(); } } //Thread executing read Data operation public class ReaderThread extends Thread{ private final Data data; public ReaderThread(Data data){ this.data=data; } public void run(){ try{ while(true){ char[] readBuf=data.read(); System.out.println(Thread.currentThread().getName()+" reads "+String.valueOf(readBuf)); } }catch(InterruptedException e){ e.printStackTrace(); } } } //Threads that perform write Data operations public class WriterThread extends Thread { private static final Random random=new Random(); private final Data data; private final String filler; private int index=0; public WriterThread(Data data,String filler){ this.data=data; this.filler=filler; } public void run(){ try{ while(true){ char c=nextChar(); data.write(c); Thread.sleep(random.nextInt(3000)); } }catch(InterruptedException e){ e.printStackTrace(); } } private char nextChar(){ char c=filler.charAt(index); index++; if(index>=filler.length()){ index=0; } return c; } } //Startup class public class Tester { public static void main(String[] args){ Data data=new Data(10); new ReaderThread(data).start(); new ReaderThread(data).start(); new ReaderThread(data).start(); new ReaderThread(data).start(); new ReaderThread(data).start(); new WriterThread(data,"ABCDEFGHIJKLMNOPQRSTUVWXYZ").start(); new WriterThread(data,"abcdefghijklmnopqrstuvwxyz").start(); } }
Understanding of read write lock mode
In fact, read write lock mode classifies the access to resources under multithreading, so as to obtain two kinds of logical locks; for multiple "read" threads that do not change the state of objects, they do not execute mutual exclusion, so as to eliminate the performance loss caused by the execution of mutual exclusion;