Resolves checkstyle errors for patterns starting with letter r (#1072)
* Reduces checkstyle errors in reactor * Reduces checkstyle errors in reader-writer-lock * Reduces checkstyle errors in repository * Reduces checkstyle errors in resource-acquisition-is-initialization * Reduces checkstyle errors in retry
This commit is contained in:
committed by
Ilkka Seppälä
parent
4dae1fae57
commit
9c8ad4485b
@ -28,26 +28,23 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* In a multiple thread applications, the threads may try to synchronize the shared resources
|
||||
* regardless of read or write operation. It leads to a low performance especially in a "read more
|
||||
* write less" system as indeed the read operations are thread-safe to another read operation.
|
||||
* <p>
|
||||
* Reader writer lock is a synchronization primitive that try to resolve this problem. This pattern
|
||||
* allows concurrent access for read-only operations, while write operations require exclusive
|
||||
* access. This means that multiple threads can read the data in parallel but an exclusive lock is
|
||||
* needed for writing or modifying data. When a writer is writing the data, all other writers or
|
||||
* readers will be blocked until the writer is finished writing.
|
||||
*
|
||||
* <p>
|
||||
* This example use two mutex to demonstrate the concurrent access of multiple readers and writers.
|
||||
*
|
||||
*
|
||||
*
|
||||
* <p>Reader writer lock is a synchronization primitive that try to resolve this problem. This
|
||||
* pattern allows concurrent access for read-only operations, while write operations require
|
||||
* exclusive access. This means that multiple threads can read the data in parallel but an exclusive
|
||||
* lock is needed for writing or modifying data. When a writer is writing the data, all other
|
||||
* writers or readers will be blocked until the writer is finished writing.
|
||||
*
|
||||
* <p>This example use two mutex to demonstrate the concurrent access of multiple readers and
|
||||
* writers.
|
||||
*
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class App {
|
||||
@ -55,27 +52,27 @@ public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(10);
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
|
||||
|
||||
// Start writers
|
||||
IntStream.range(0, 5)
|
||||
.forEach(i -> executeService.submit(new Writer("Writer " + i, lock.writeLock(),
|
||||
.forEach(i -> executeService.submit(new Writer("Writer " + i, lock.writeLock(),
|
||||
ThreadLocalRandom.current().nextLong(5000))));
|
||||
LOGGER.info("Writers added...");
|
||||
|
||||
// Start readers
|
||||
IntStream.range(0, 5)
|
||||
.forEach(i -> executeService.submit(new Reader("Reader " + i, lock.readLock(),
|
||||
.forEach(i -> executeService.submit(new Reader("Reader " + i, lock.readLock(),
|
||||
ThreadLocalRandom.current().nextLong(10))));
|
||||
LOGGER.info("Readers added...");
|
||||
|
||||
|
||||
try {
|
||||
Thread.sleep(5000L);
|
||||
} catch (InterruptedException e) {
|
||||
@ -85,11 +82,10 @@ public class App {
|
||||
|
||||
// Start readers
|
||||
IntStream.range(6, 10)
|
||||
.forEach(i -> executeService.submit(new Reader("Reader " + i, lock.readLock(),
|
||||
.forEach(i -> executeService.submit(new Reader("Reader " + i, lock.readLock(),
|
||||
ThreadLocalRandom.current().nextLong(10))));
|
||||
LOGGER.info("More readers added...");
|
||||
|
||||
|
||||
|
||||
|
||||
// In the system console, it can see that the read operations are executed concurrently while
|
||||
// write operations are exclusive.
|
||||
|
@ -24,12 +24,11 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Reader class, read when it acquired the read lock
|
||||
* Reader class, read when it acquired the read lock.
|
||||
*/
|
||||
public class Reader implements Runnable {
|
||||
|
||||
@ -38,14 +37,14 @@ public class Reader implements Runnable {
|
||||
private Lock readLock;
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
private long readingTime;
|
||||
|
||||
/**
|
||||
* Create new Reader
|
||||
*
|
||||
* @param name - Name of the thread owning the reader
|
||||
* @param readLock - Lock for this reader
|
||||
* Create new Reader.
|
||||
*
|
||||
* @param name - Name of the thread owning the reader
|
||||
* @param readLock - Lock for this reader
|
||||
* @param readingTime - amount of time (in milliseconds) for this reader to engage reading
|
||||
*/
|
||||
public Reader(String name, Lock readLock, long readingTime) {
|
||||
@ -53,11 +52,11 @@ public class Reader implements Runnable {
|
||||
this.readLock = readLock;
|
||||
this.readingTime = readingTime;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create new Reader who reads for 250ms
|
||||
*
|
||||
* @param name - Name of the thread owning the reader
|
||||
* Create new Reader who reads for 250ms.
|
||||
*
|
||||
* @param name - Name of the thread owning the reader
|
||||
* @param readLock - Lock for this reader
|
||||
*/
|
||||
public Reader(String name, Lock readLock) {
|
||||
@ -78,8 +77,7 @@ public class Reader implements Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate the read operation
|
||||
*
|
||||
* Simulate the read operation.
|
||||
*/
|
||||
public void read() throws InterruptedException {
|
||||
LOGGER.info("{} begin", name);
|
||||
|
@ -29,18 +29,17 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Class responsible for control the access for reader or writer
|
||||
*
|
||||
* Allows multiple readers to hold the lock at same time, but if any writer holds the lock then readers wait. If reader
|
||||
* holds the lock then writer waits. This lock is not fair.
|
||||
*
|
||||
* <p>Allows multiple readers to hold the lock at same time, but if any writer holds the lock then
|
||||
* readers wait. If reader holds the lock then writer waits. This lock is not fair.
|
||||
*/
|
||||
public class ReaderWriterLock implements ReadWriteLock {
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderWriterLock.class);
|
||||
|
||||
|
||||
@ -50,13 +49,13 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
|
||||
/**
|
||||
* Global mutex is used to indicate that whether reader or writer gets the lock in the moment.
|
||||
* <p>
|
||||
* 1. When it contains the reference of {@link #readerLock}, it means that the lock is acquired by the reader, another
|
||||
* reader can also do the read operation concurrently. <br>
|
||||
* 2. When it contains the reference of reference of {@link #writerLock}, it means that the lock is acquired by the
|
||||
* writer exclusively, no more reader or writer can get the lock.
|
||||
* <p>
|
||||
* This is the most important field in this class to control the access for reader/writer.
|
||||
*
|
||||
* <p>1. When it contains the reference of {@link #readerLock}, it means that the lock is
|
||||
* acquired by the reader, another reader can also do the read operation concurrently. <br> 2.
|
||||
* When it contains the reference of reference of {@link #writerLock}, it means that the lock is
|
||||
* acquired by the writer exclusively, no more reader or writer can get the lock.
|
||||
*
|
||||
* <p>This is the most important field in this class to control the access for reader/writer.
|
||||
*/
|
||||
private Set<Object> globalMutex = new HashSet<>();
|
||||
|
||||
@ -74,22 +73,21 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
/**
|
||||
* return true when globalMutex hold the reference of writerLock
|
||||
* return true when globalMutex hold the reference of writerLock.
|
||||
*/
|
||||
private boolean doesWriterOwnThisLock() {
|
||||
return globalMutex.contains(writerLock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nobody get the lock when globalMutex contains nothing
|
||||
*
|
||||
* Nobody get the lock when globalMutex contains nothing.
|
||||
*/
|
||||
private boolean isLockFree() {
|
||||
return globalMutex.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reader Lock, can be access for more than one reader concurrently if no writer get the lock
|
||||
* Reader Lock, can be access for more than one reader concurrently if no writer get the lock.
|
||||
*/
|
||||
private class ReadLock implements Lock {
|
||||
|
||||
@ -104,8 +102,8 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
/**
|
||||
* Acquire the globalMutex lock on behalf of current and future concurrent readers. Make sure no writers currently
|
||||
* owns the lock.
|
||||
* Acquire the globalMutex lock on behalf of current and future concurrent readers. Make sure no
|
||||
* writers currently owns the lock.
|
||||
*/
|
||||
private void acquireForReaders() {
|
||||
// Try to get the globalMutex lock for the first reader
|
||||
@ -116,7 +114,8 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
try {
|
||||
globalMutex.wait();
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.info("InterruptedException while waiting for globalMutex in acquireForReaders", e);
|
||||
LOGGER
|
||||
.info("InterruptedException while waiting for globalMutex in acquireForReaders", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
@ -165,7 +164,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
/**
|
||||
* Writer Lock, can only be accessed by one writer concurrently
|
||||
* Writer Lock, can only be accessed by one writer concurrently.
|
||||
*/
|
||||
private class WriteLock implements Lock {
|
||||
|
||||
|
@ -24,12 +24,11 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Writer class, write when it acquired the write lock
|
||||
* Writer class, write when it acquired the write lock.
|
||||
*/
|
||||
public class Writer implements Runnable {
|
||||
|
||||
@ -38,24 +37,24 @@ public class Writer implements Runnable {
|
||||
private Lock writeLock;
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
private long writingTime;
|
||||
|
||||
/**
|
||||
* Create new Writer who writes for 250ms
|
||||
*
|
||||
* @param name - Name of the thread owning the writer
|
||||
* Create new Writer who writes for 250ms.
|
||||
*
|
||||
* @param name - Name of the thread owning the writer
|
||||
* @param writeLock - Lock for this writer
|
||||
*/
|
||||
public Writer(String name, Lock writeLock) {
|
||||
this(name, writeLock, 250L);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create new Writer
|
||||
*
|
||||
* @param name - Name of the thread owning the writer
|
||||
* @param writeLock - Lock for this writer
|
||||
* Create new Writer.
|
||||
*
|
||||
* @param name - Name of the thread owning the writer
|
||||
* @param writeLock - Lock for this writer
|
||||
* @param writingTime - amount of time (in milliseconds) for this reader to engage writing
|
||||
*/
|
||||
public Writer(String name, Lock writeLock, long writingTime) {
|
||||
@ -77,9 +76,9 @@ public class Writer implements Runnable {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simulate the write operation
|
||||
* Simulate the write operation.
|
||||
*/
|
||||
public void write() throws InterruptedException {
|
||||
LOGGER.info("{} begin", name);
|
||||
|
Reference in New Issue
Block a user