Multi-threaded JUC Season 2 Read-write lock ReentrantReadWriteLock

A read-write lock

1.1 ReentrantReadWriteLock

The reentrantReadWriteloc solution mainly solves read-read sharing, read-write mutual exclusion. In the scenario of more reads and less writes, read-write locks have higher performance.

1.2 Case Code

1. Resource class

package com.ljf.thread.readwritelock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @ClassName: MyResources
 * @Description: TODO
 * @Author: admin
 * @Date: 2023/05/21 11:33:08
 * @Version: V1.0
 **/
public class MyResources {
    ReentrantLock mylock = new ReentrantLock();
    Map<String,String> resultMap=new HashMap<>();
    ReadWriteLock myrwLock=new ReentrantReadWriteLock();
    public void writeSomething(String key,String value){
           myrwLock.writeLock().lock();
        try {
            System.out.println("Thread: " + Thread.currentThread().getName() + "Writing...");
            Thread. sleep(500);
            resultMap. put(key, value);
            System.out.println("Thread: " + Thread.currentThread().getName() + "Write completed...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            myrwLock.writeLock().unlock();

        }
    }
    public void readSomething(String key){
        myrwLock. readLock(). lock();
        try {
            System.out.println("Thread: " + Thread.currentThread().getName() + "Reading...");
            resultMap. get(key);
            Thread. sleep(200);
            System.out.println("Thread: " + Thread.currentThread().getName() + "Read completed...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            myrwLock.readLock().unlock();
        }
    }
}

2. Call class

package com.ljf.thread.readwritelock;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
         MyResources myResources = new MyResources();
        for(int k=0;k<10;k++){
            final int m=k;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myResources.writeSomething("do" + m,m + "");
                }
            }).start();
        }
        System.out.println("=====");
        for(int k=0;k<10;k++){
            final int m=k;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myResources. readSomething("do" + m);
                }
            }).start();
        }
    }
}

3. Execution results: The writing threads are mutually exclusive, one thread finishes writing, and the other writes; the reading thread is shared, and multiple threads can read at the same time

Hello World!
Thread: Thread-1 is writing…
=====
Thread: Thread-1 write complete…
Thread: Thread-3 is writing…
Thread: Thread-3 write complete…
Thread: Thread-0 is writing…
Thread: Thread-0 write complete…
Thread: Thread-2 is writing…
Thread: Thread-2 write complete…
Thread: Thread-4 is writing…
Thread: Thread-4 write complete…
Thread: Thread-5 is writing…
Thread: Thread-5 write complete…
Thread: Thread-6 is writing…
Thread: Thread-6 write complete…
Thread: Thread-7 is writing…
Thread: Thread-7 write complete…
Thread: Thread-8 is writing…
Thread: Thread-8 write complete…
Thread: Thread-9 is writing…
Thread: Thread-9 write complete…
Thread: Thread-10 is reading…
Thread: Thread-11 is reading…
Thread: Thread-12 is reading…
Thread: Thread-13 is reading…
Thread: Thread-14 is reading…
Thread: Thread-15 is reading…
Thread: Thread-17 is reading…
Thread: Thread-18 is reading…
Thread: Thread-16 is reading…
Thread: Thread-19 is reading…
Thread: Thread-15 Read complete…
Thread: Thread-19 Read complete…
Thread: Thread-16 Read complete…
Thread: Thread-12 Read complete…
Thread: Thread-14 Read complete…
Thread: Thread-11 Read complete…
Thread: Thread-13 Read complete…
Thread: Thread-10 read complete…
Thread: Thread-18 Read complete…
Thread: Thread-17 Read complete…

1.3 Case Code

Call class

package com.ljf.thread.readwritelock;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
         MyResources myResources = new MyResources();
        for(int k=0;k<10;k++){
            final int m=k;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myResources.writeSomething("do" + m,m + "");
                }
            }).start();
        }
        System.out.println("=====");
        for(int k=0;k<10;k++){
            final int m=k;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myResources. readSomething("do" + m);
                }
            }).start();
        }

        for (int i = 1; i <=3; i ++ ) {
            final int m=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myResources.writeSomething("do" + m,m + "");
                }
            }).start();
        }

    }


}

Result: Read-write mutual exclusion, read-read sharing, when the read thread is not completed, other write threads cannot acquire the lock.

Thread: Thread-0 is writing…
=====
Thread: Thread-0 write complete…
Thread: Thread-1 is writing…
Thread: Thread-1 write complete…
Thread: Thread-2 is writing…
Thread: Thread-2 write complete…
Thread: Thread-3 is writing…
Thread: Thread-3 write complete…
Thread: Thread-4 is writing…
Thread: Thread-4 write complete…
Thread: Thread-5 is writing…
Thread: Thread-5 write complete…
Thread: Thread-6 is writing…
Thread: Thread-6 write complete…
Thread: Thread-7 is writing…
Thread: Thread-7 write complete…
Thread: Thread-8 is writing…
Thread: Thread-8 write complete…
Thread: Thread-9 is writing…
Thread: Thread-9 write complete…
Thread: Thread-10 is reading…
Thread: Thread-11 is reading…
Thread: Thread-12 is reading…
Thread: Thread-13 is reading…
Thread: Thread-14 is reading…
Thread: Thread-16 is reading…
Thread: Thread-15 is reading…
Thread: Thread-17 is reading…
Thread: Thread-19 is reading…
Thread: Thread-18 is reading…
Thread: Thread-11 Read complete…
Thread: Thread-10 read complete…
Thread: Thread-12 Read complete…
Thread: Thread-16 Read complete…
Thread: Thread-14 Read complete…
Thread: Thread-18 Read complete…
Thread: Thread-19 Read complete…
Thread: Thread-13 Read complete…
Thread: Thread-17 Read complete…
Thread: Thread-15 Read complete…
Thread: Thread-20 is writing…
Thread: Thread-20 write complete…
Thread: Thread-21 is writing…
Thread: Thread-21 Write complete…
Thread: Thread-22 is writing…
Thread: Thread-22 write completed…