mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-18 00:59:37 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -35,10 +35,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* lock. Only if the locking criterion check indicates that locking is required does the actual
|
||||
* locking logic proceed.
|
||||
*
|
||||
* <p>In {@link Inventory} we store the items with a given size. However, we do not store more
|
||||
* items than the inventory size. To address concurrent access problems we use double checked
|
||||
* locking to add item to inventory. In this method, the thread which gets the lock first adds the
|
||||
* item.
|
||||
* <p>In {@link Inventory} we store the items with a given size. However, we do not store more items
|
||||
* than the inventory size. To address concurrent access problems we use double checked locking to
|
||||
* add item to inventory. In this method, the thread which gets the lock first adds the item.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -51,11 +50,15 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
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);
|
||||
IntStream.range(0, 3)
|
||||
.<Runnable>mapToObj(
|
||||
i ->
|
||||
() -> {
|
||||
while (inventory.addItem(new Item())) {
|
||||
LOGGER.info("Adding another item");
|
||||
}
|
||||
})
|
||||
.forEach(executorService::execute);
|
||||
|
||||
executorService.shutdown();
|
||||
try {
|
||||
|
||||
+3
-10
@@ -30,9 +30,7 @@ import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Inventory.
|
||||
*/
|
||||
/** Inventory. */
|
||||
@Slf4j
|
||||
public class Inventory {
|
||||
|
||||
@@ -40,18 +38,14 @@ public class Inventory {
|
||||
private final List<Item> items;
|
||||
private final Lock lock;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public Inventory(int inventorySize) {
|
||||
this.inventorySize = inventorySize;
|
||||
this.items = new ArrayList<>(inventorySize);
|
||||
this.lock = new ReentrantLock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add item.
|
||||
*/
|
||||
/** Add item. */
|
||||
public boolean addItem(Item item) {
|
||||
if (items.size() < inventorySize) {
|
||||
lock.lock();
|
||||
@@ -77,5 +71,4 @@ public class Inventory {
|
||||
public final List<Item> getItems() {
|
||||
return List.copyOf(items);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,8 +24,5 @@
|
||||
*/
|
||||
package com.iluwatar.doublechecked.locking;
|
||||
|
||||
/**
|
||||
* Item.
|
||||
*/
|
||||
public class Item {
|
||||
}
|
||||
/** Item. */
|
||||
public class Item {}
|
||||
|
||||
Reference in New Issue
Block a user