Java 11 migrate c-d (remaining) (#1111)
* Moves converter pattern to Java 11 * Moves cqrs pattern to Java 11 * Moves dao pattern to Java 11 * Moves data-bus pattern to Java 11 * Moves data-locality pattern to Java 11 * Moves data-mapper pattern to Java 11 * Moves data-transfer-object pattern to Java 11 * Moves decorator pattern to Java 11 * Moves delegation pattern to Java 11 * Moves dependency-injection to Java 11 * Moves dirty-flag to Java 11 * Moves double-buffer to Java 11 * Moves double-checked-locking to Java 11 * Moves double-dispatch to Java 11 * Corrects with changes thats breaking test cases
This commit is contained in:
committed by
Ilkka Seppälä
parent
5681684157
commit
ea57934db6
@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.doublechecked.locking;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -50,15 +50,13 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
final Inventory inventory = new Inventory(1000);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
executorService.execute(() -> {
|
||||
while (inventory.addItem(new Item())) {
|
||||
LOGGER.info("Adding another item");
|
||||
}
|
||||
});
|
||||
}
|
||||
final var inventory = new Inventory(1000);
|
||||
var executorService = Executors.newFixedThreadPool(3);
|
||||
IntStream.range(0, 3).<Runnable>mapToObj(i -> () -> {
|
||||
while (inventory.addItem(new Item())) {
|
||||
LOGGER.info("Adding another item");
|
||||
}
|
||||
}).forEach(executorService::execute);
|
||||
|
||||
executorService.shutdown();
|
||||
try {
|
||||
|
@ -24,7 +24,6 @@
|
||||
package com.iluwatar.doublechecked.locking;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@ -60,8 +59,8 @@ public class Inventory {
|
||||
try {
|
||||
if (items.size() < inventorySize) {
|
||||
items.add(item);
|
||||
LOGGER.info("{}: items.size()={}, inventorySize={}", Thread.currentThread(), items
|
||||
.size(), inventorySize);
|
||||
var thread = Thread.currentThread();
|
||||
LOGGER.info("{}: items.size()={}, inventorySize={}", thread, items.size(), inventorySize);
|
||||
return true;
|
||||
}
|
||||
} finally {
|
||||
@ -77,7 +76,7 @@ public class Inventory {
|
||||
* @return All the items of the inventory, as an unmodifiable list
|
||||
*/
|
||||
public final List<Item> getItems() {
|
||||
return Collections.unmodifiableList(items);
|
||||
return List.copyOf(items);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,15 +26,12 @@ package com.iluwatar.doublechecked.locking;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,25 +23,24 @@
|
||||
|
||||
package com.iluwatar.doublechecked.locking;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Date: 12/10/15 - 9:34 PM
|
||||
@ -83,34 +82,32 @@ public class InventoryTest {
|
||||
public void testAddItem() throws Exception {
|
||||
assertTimeout(ofMillis(10000), () -> {
|
||||
// Create a new inventory with a limit of 1000 items and put some load on the add method
|
||||
final Inventory inventory = new Inventory(INVENTORY_SIZE);
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
executorService.execute(() -> {
|
||||
while (inventory.addItem(new Item())) {};
|
||||
});
|
||||
}
|
||||
final var inventory = new Inventory(INVENTORY_SIZE);
|
||||
final var executorService = Executors.newFixedThreadPool(THREAD_COUNT);
|
||||
IntStream.range(0, THREAD_COUNT).<Runnable>mapToObj(i -> () -> {
|
||||
while (inventory.addItem(new Item())) ;
|
||||
}).forEach(executorService::execute);
|
||||
|
||||
// Wait until all threads have finished
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
|
||||
// Check the number of items in the inventory. It should not have exceeded the allowed maximum
|
||||
final List<Item> items = inventory.getItems();
|
||||
final var items = inventory.getItems();
|
||||
assertNotNull(items);
|
||||
assertEquals(INVENTORY_SIZE, items.size());
|
||||
|
||||
assertEquals(INVENTORY_SIZE, appender.getLogSize());
|
||||
|
||||
// ... and check if the inventory size is increasing continuously
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
assertTrue(appender.log.get(i).getFormattedMessage().contains("items.size()=" + (i + 1)));
|
||||
}
|
||||
IntStream.range(0, items.size())
|
||||
.mapToObj(i -> appender.log.get(i).getFormattedMessage()
|
||||
.contains("items.size()=" + (i + 1)))
|
||||
.forEach(Assertions::assertTrue);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
private List<ILoggingEvent> log = new LinkedList<>();
|
||||
|
||||
|
Reference in New Issue
Block a user