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:
Anurag Agarwal
2020-01-04 22:13:12 +05:30
committed by Ilkka Seppälä
parent cd2a2e7711
commit 20ea465b7f
52 changed files with 424 additions and 554 deletions
@@ -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();
}