📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -26,8 +26,7 @@ package com.iluwatar.reader.writer.lock;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* In a multiple thread applications, the threads may try to synchronize the shared resources
|
||||
@ -45,10 +44,9 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
|
@ -24,16 +24,14 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Reader class, read when it acquired the read lock.
|
||||
*/
|
||||
@Slf4j
|
||||
public class Reader implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Reader.class);
|
||||
|
||||
private final Lock readLock;
|
||||
|
||||
private final String name;
|
||||
|
@ -29,8 +29,7 @@ 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;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Class responsible for control the access for reader or writer
|
||||
@ -38,10 +37,9 @@ import org.slf4j.LoggerFactory;
|
||||
* <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.
|
||||
*/
|
||||
@Slf4j
|
||||
public class ReaderWriterLock implements ReadWriteLock {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderWriterLock.class);
|
||||
|
||||
|
||||
private final Object readerMutex = new Object();
|
||||
|
||||
|
@ -24,16 +24,14 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Writer class, write when it acquired the write lock.
|
||||
*/
|
||||
@Slf4j
|
||||
public class Writer implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Writer.class);
|
||||
|
||||
private final Lock writeLock;
|
||||
|
||||
private final String name;
|
||||
|
@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderAndWriterTest {
|
||||
class ReaderAndWriterTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@ -57,7 +57,7 @@ public class ReaderAndWriterTest {
|
||||
* Verify reader and writer can only get the lock to read and write orderly
|
||||
*/
|
||||
@Test
|
||||
public void testReadAndWrite() throws Exception {
|
||||
void testReadAndWrite() throws Exception {
|
||||
|
||||
var lock = new ReaderWriterLock();
|
||||
|
||||
@ -87,7 +87,7 @@ public class ReaderAndWriterTest {
|
||||
* Verify reader and writer can only get the lock to read and write orderly
|
||||
*/
|
||||
@Test
|
||||
public void testWriteAndRead() throws Exception {
|
||||
void testWriteAndRead() throws Exception {
|
||||
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderTest {
|
||||
class ReaderTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@ -58,7 +58,7 @@ public class ReaderTest {
|
||||
* Verify that multiple readers can get the read lock concurrently
|
||||
*/
|
||||
@Test
|
||||
public void testRead() throws Exception {
|
||||
void testRead() throws Exception {
|
||||
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class WriterTest {
|
||||
class WriterTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@ -58,7 +58,7 @@ public class WriterTest {
|
||||
* Verify that multiple writers will get the lock in order.
|
||||
*/
|
||||
@Test
|
||||
public void testWrite() throws Exception {
|
||||
void testWrite() throws Exception {
|
||||
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
Reference in New Issue
Block a user