Java 11 migrate remaining q-r (#1121)
* Moves queue-load-leveling to Java 11 * Moves reactor to Java 11 * Moves reader-writer-lock to Java 11 * Moves repository to Java 11 * Moves resource-acquisition-is-initialization to Java 11 * Moves retry to Java 11 * Moves role-object to Java 11
This commit is contained in:
committed by
Ilkka Seppälä
parent
cd2a2e7711
commit
20ea465b7f
@ -23,11 +23,9 @@
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
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;
|
||||
|
||||
@ -58,19 +56,21 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(10);
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
var executeService = Executors.newFixedThreadPool(10);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
||||
// Start writers
|
||||
IntStream.range(0, 5)
|
||||
.forEach(i -> executeService.submit(new Writer("Writer " + i, lock.writeLock(),
|
||||
ThreadLocalRandom.current().nextLong(5000))));
|
||||
for (var i = 0; i < 5; i++) {
|
||||
var writingTime = ThreadLocalRandom.current().nextLong(5000);
|
||||
executeService.submit(new Writer("Writer " + i, lock.writeLock(), writingTime));
|
||||
}
|
||||
LOGGER.info("Writers added...");
|
||||
|
||||
// Start readers
|
||||
IntStream.range(0, 5)
|
||||
.forEach(i -> executeService.submit(new Reader("Reader " + i, lock.readLock(),
|
||||
ThreadLocalRandom.current().nextLong(10))));
|
||||
for (var i = 0; i < 5; i++) {
|
||||
var readingTime = ThreadLocalRandom.current().nextLong(10);
|
||||
executeService.submit(new Reader("Reader " + i, lock.readLock(), readingTime));
|
||||
}
|
||||
LOGGER.info("Readers added...");
|
||||
|
||||
try {
|
||||
@ -81,9 +81,10 @@ public class App {
|
||||
}
|
||||
|
||||
// Start readers
|
||||
IntStream.range(6, 10)
|
||||
.forEach(i -> executeService.submit(new Reader("Reader " + i, lock.readLock(),
|
||||
ThreadLocalRandom.current().nextLong(10))));
|
||||
for (var i = 6; i < 10; i++) {
|
||||
var readingTime = ThreadLocalRandom.current().nextLong(10);
|
||||
executeService.submit(new Reader("Reader " + i, lock.readLock(), readingTime));
|
||||
}
|
||||
LOGGER.info("More readers added...");
|
||||
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderWriterLock.class);
|
||||
|
||||
|
||||
private Object readerMutex = new Object();
|
||||
private final Object readerMutex = new Object();
|
||||
|
||||
private int currentReaderCount;
|
||||
|
||||
@ -57,7 +57,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
*
|
||||
* <p>This is the most important field in this class to control the access for reader/writer.
|
||||
*/
|
||||
private Set<Object> globalMutex = new HashSet<>();
|
||||
private final Set<Object> globalMutex = new HashSet<>();
|
||||
|
||||
private ReadLock readerLock = new ReadLock();
|
||||
private WriteLock writerLock = new WriteLock();
|
||||
@ -114,8 +114,8 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
try {
|
||||
globalMutex.wait();
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER
|
||||
.info("InterruptedException while waiting for globalMutex in acquireForReaders", e);
|
||||
var message = "InterruptedException while waiting for globalMutex in acquireForReaders";
|
||||
LOGGER.info(message, e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
@ -125,7 +125,6 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
|
||||
synchronized (readerMutex) {
|
||||
currentReaderCount--;
|
||||
// Release the lock only when it is the last reader, it is ensure that the lock is released
|
||||
@ -142,7 +141,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockInterruptibly() throws InterruptedException {
|
||||
public void lockInterruptibly() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@ -152,7 +151,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
|
||||
public boolean tryLock(long time, TimeUnit unit) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@ -170,7 +169,6 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
|
||||
@Override
|
||||
public void lock() {
|
||||
|
||||
synchronized (globalMutex) {
|
||||
|
||||
// Wait until the lock is free.
|
||||
@ -189,7 +187,6 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
|
||||
synchronized (globalMutex) {
|
||||
globalMutex.remove(this);
|
||||
// Notify the waiter, other writer or reader
|
||||
@ -198,7 +195,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockInterruptibly() throws InterruptedException {
|
||||
public void lockInterruptibly() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@ -208,7 +205,7 @@ public class ReaderWriterLock implements ReadWriteLock {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
|
||||
public boolean tryLock(long time, TimeUnit unit) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
@ -31,9 +31,8 @@ import org.junit.jupiter.api.Test;
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,19 +23,17 @@
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.reader.writer.lock.utils.InMemoryAppender;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
@ -61,12 +59,12 @@ public class ReaderAndWriterTest {
|
||||
@Test
|
||||
public void testReadAndWrite() throws Exception {
|
||||
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
var lock = new ReaderWriterLock();
|
||||
|
||||
Reader reader1 = new Reader("Reader 1", lock.readLock());
|
||||
Writer writer1 = new Writer("Writer 1", lock.writeLock());
|
||||
var reader1 = new Reader("Reader 1", lock.readLock());
|
||||
var writer1 = new Writer("Writer 1", lock.writeLock());
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(2);
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
executeService.submit(reader1);
|
||||
// Let reader1 execute first
|
||||
Thread.sleep(150);
|
||||
@ -91,11 +89,11 @@ public class ReaderAndWriterTest {
|
||||
@Test
|
||||
public void testWriteAndRead() throws Exception {
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(2);
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
||||
Reader reader1 = new Reader("Reader 1", lock.readLock());
|
||||
Writer writer1 = new Writer("Writer 1", lock.writeLock());
|
||||
var reader1 = new Reader("Reader 1", lock.readLock());
|
||||
var writer1 = new Writer("Writer 1", lock.writeLock());
|
||||
|
||||
executeService.submit(writer1);
|
||||
// Let writer1 execute first
|
||||
|
@ -23,20 +23,18 @@
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import com.iluwatar.reader.writer.lock.utils.InMemoryAppender;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
@ -62,11 +60,11 @@ public class ReaderTest {
|
||||
@Test
|
||||
public void testRead() throws Exception {
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(2);
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
||||
Reader reader1 = spy(new Reader("Reader 1", lock.readLock()));
|
||||
Reader reader2 = spy(new Reader("Reader 2", lock.readLock()));
|
||||
var reader1 = spy(new Reader("Reader 1", lock.readLock()));
|
||||
var reader2 = spy(new Reader("Reader 2", lock.readLock()));
|
||||
|
||||
executeService.submit(reader1);
|
||||
Thread.sleep(150);
|
||||
|
@ -23,20 +23,18 @@
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import com.iluwatar.reader.writer.lock.utils.InMemoryAppender;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
@ -62,11 +60,11 @@ public class WriterTest {
|
||||
@Test
|
||||
public void testWrite() throws Exception {
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(2);
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
var executeService = Executors.newFixedThreadPool(2);
|
||||
var lock = new ReaderWriterLock();
|
||||
|
||||
Writer writer1 = spy(new Writer("Writer 1", lock.writeLock()));
|
||||
Writer writer2 = spy(new Writer("Writer 2", lock.writeLock()));
|
||||
var writer1 = spy(new Writer("Writer 1", lock.writeLock()));
|
||||
var writer2 = spy(new Writer("Writer 2", lock.writeLock()));
|
||||
|
||||
executeService.submit(writer1);
|
||||
// Let write1 execute first
|
||||
|
@ -26,10 +26,9 @@ package com.iluwatar.reader.writer.lock.utils;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* InMemory Log Appender Util.
|
||||
|
Reference in New Issue
Block a user