Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)

javathread.jpeg

Read-Write Lock pattern

Even if the thread "reads" the state of the instance, the state of the instance does not change. It changes when the process of "writing" is performed.

Reading thread: ** It doesn't matter if multiple threads read at the same time. But I can't write while reading. ** **

Writing thread: ** While one thread is writing, no other thread can read or write. ** **

In general, exclusive control reduces performance, but performance can be improved by considering exclusive control for writing and exclusive control for reading separately.

Consider reading and writing from multiple threads to an instance of a class called Data. When a thread tries to secure a lock to read

  1. If the thread you are writing already exists, you will be kept waiting.
  2. You can't wait if the thread you're reading already exists. When a thread tries to secure a lock to write
  3. If the thread you are writing already exists, you will be kept waiting.
  4. If the thread you are reading already exists, you will be kept waiting.

(See this manual for the entire code)

ReadWriteLock.java


public final 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(); 
    } 
}

The significance of the waitingWriters field If the number of ReaderThread threads is larger than the number of WriterThread threads, only Reads will be made and Writes will be difficult. By making the ReaderThread thread wait when waitingWriters> 0 holds, it is possible to prevent the phenomenon that WriterThread cannot start execution.

The significance of the preferWriter field However, just considering waitingWriters may prevent Reader Thread from starting execution. Therefore, the preferWriter feel defines whether to prioritize Reader Thread or Writer Thread. In the sample program, it is set to true when reading is completed and false when writing is completed.

Character

Reader role The Reader role reads the SharedResourece role. In the sample program, this is the ReaderThread class.

Writer role The Writer role writes to the SharedResourece role. In the sample program, this is the WriterThread class.

Shared Resource role The SharedResource role represents a resource shared by the Redaer role and the Writer role. The SharedResource role provides a process that does not change the internal state (read) and a process that changes the internal state (write). In the sample program, this is the Data class.

ReadWriteLock role The ReadWriteLock role provides a lock for the SharedResource role to realize read processing and write processing. In the sample program, this is the ReadWriteLock class.


Relation Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 1) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 2) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 3) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 6) Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)

Recommended Posts

Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 3)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
[Java] Summary of design patterns
A quick review of Java learned in class part4
Summary of Java language basics
A quick review of Java learned in class part3
A quick review of Java learned in class part2
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
Java Design Patterns
What I learned in Java (Part 2) What are variables?
A quick review of Java learned in class
Summary of what I learned in Spring Batch
Try design patterns in C language! Memento pattern-Let's memorize the memories of the data
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
What I learned in Java (Part 3) Instruction execution statement
Summary of how to implement default arguments in Java
Summary of Java support 2018
Java design pattern summary
What I learned in Java (Part 4) Conditional branching and repetition
[Java] Basic summary of Java not covered by Progate ~ Part 2 ยท List ~
Road to Java Engineer Part2 What kind of language is Java?
Read design patterns in Ruby
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Summary of for statements
Summary of Java Math class
Implementation of gzip in java
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of mathematical operations
What I have learned in Java (Part 1) Java development flow and overview
Summary of ORM "uroboroSQL" that can be used in enterprise Java
[For beginners] Summary of java constructor
Summary of [Java silver study] package
Basic usage of java Optional Part 1
AtCoder 400 points algorithm summary (Java edition)
List of members added in Java 9
Creating lexical analysis in Java 8 (Part 2)
List of types added in Java 9
Summary of object-oriented programming using Java
Implementation of like function in Java
Creating lexical analysis in Java 8 (Part 1)