Introduction to Reentrant ReadWriteLock

Keywords: Java jvm

Once synchronized modification is added to the method of the object, only one thread can access the synchronized modification method at any time. Assuming that a data object has write and read methods, in order to ensure the security of data in multithreaded environment, synchronized synchronized block is added to the read and write methods of the object. In this way, when any thread writes, other threads can not read and change data; if any thread reads, other threads can not read or write. When the write operation is much larger than the read operation, the problem is not big, but when the read operation is much larger than the write operation, the performance bottleneck will be caused, because in this case, the read operation can be carried out simultaneously, while the lock operation restricts the concurrent reading of data.

 

ReadWriteLock solves this problem. When writing, other threads can't read or write data. When reading, other threads can't write data, but they can read data.

 

Read-write locks: divided into read locks and write locks, multiple read locks are not mutually exclusive, read locks and write locks are mutually exclusive, which is controlled by jvm itself, you just need to put on the corresponding lock. If your code is read-only, it can be read by many people at the same time, but can not write at the same time, then read lock; if your code modifies data, only one person can write, and can not read at the same time, then write lock. In short, read the lock while reading, write the lock when writing!

          private Object data = null;//Sharing data, only one thread can write the data, but multiple threads can read the data at the same time.
             private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
             public void get(){
                 rwl.readLock().lock();//Read lock, other threads can only read but not write
                 System.out.println(Thread.currentThread().getName() + " be ready to read data!");
                 try {
                     Thread.sleep((long)(Math.random()*1000));
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 System.out.println(Thread.currentThread().getName() + "have read data :" + data);
                 rwl.readLock().unlock(); //Release the read lock, preferably in finnaly inside
             }
        
             public void put(Object data){
        
                 rwl.writeLock().lock();//Up-write locks, no other threads are allowed to read or write
                 System.out.println(Thread.currentThread().getName() + " be ready to write data!");
                 try {
                     Thread.sleep((long)(Math.random()*1000));
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 this.data = data;
                 System.out.println(Thread.currentThread().getName() + " have write data: " + data);
        
                 rwl.writeLock().unlock();//Release write lock    
             }
         }

Posted by Jewbilee on Mon, 28 Jan 2019 12:45:14 -0800