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
(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.
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