Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions week0/BeforeOrderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.hanghae.radis.service;

import com.hanghae.radis.model.OrderInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class BeforeOrderService {
// 상품 DB (공유 자원의 동시성 이슈 해결을 위해 ConcurrentMap 사용)
private final ConcurrentMap<String, Integer> productDatabase = new ConcurrentHashMap<>();
// 가장 최근 주문 정보를 저장하는 DB
private final Map<String, OrderInfo> latestOrderDatabase = new HashMap<>();

public BeforeOrderService() {
// 초기 상품 데이터
productDatabase.put("apple", 100);
productDatabase.put("banana", 50);
productDatabase.put("orange", 75);
}

// 주문 처리 메서드
public void order(String productName, int amount) {
try {
Thread.sleep(1); // 동시성 이슈 유발을 위한 인위적 지연
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}

//스레드 안전성을 보장하기 위해 특정 키에 대한 연산을 원자적으로 수행하는 compute 사용
productDatabase.compute(productName, (key, value) -> {
if (value >= amount) {
//주문 정보 화면 출력
System.out.println(Thread.currentThread().getName() + " 주문정보 : \n"
+ productName + ": " + amount + " 건, 현재 수량: " + value + " , 판매 후 수량: " + (value - amount));

//주문 성공시 최신 주문내역 저장
latestOrderDatabase.put(productName, new OrderInfo(productName, amount, System.currentTimeMillis()));

return value - amount; // 상품수가 주문량보다 많을 경우 업데이트
} else {
return value; // 아닐 경우 상품수 유지
}
});
}

// 재고 조회
public int getStock(String productName) {
return productDatabase.getOrDefault(productName, 0);
}

// 최근 주문 조회
public OrderInfo getLatestOrder(String productName) {
return latestOrderDatabase.getOrDefault(productName, null);
}
}
53 changes: 53 additions & 0 deletions week0/BeforeOrderServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.hanghae.radis.service;

import org.junit.jupiter.api.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BeforeOrderServiceTest {
private final BeforeOrderService service = new BeforeOrderService();

@Test
void testConcurrentOrdersCauseStockMismatch() throws InterruptedException {
String productName = "apple";
int initialStock = service.getStock(productName);

int orderAmount = 8;
int threadCount = 100;

ExecutorService executor = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(threadCount);

// 각 스레드에서 주문을 수행하는 작업 생성
for (int i = 0; i < threadCount; i++) {
executor.execute(() -> {
try {
service.order(productName, orderAmount);
} finally {
latch.countDown(); // 작업 완료 후 카운트 감소
}
});
}

// 모든 스레드가 작업을 완료할 때까지 대기
latch.await();
executor.shutdown();

// 최종 재고 값 확인
int expectedStock = initialStock % orderAmount;
int actualStock = service.getStock(productName);

System.out.println("Expected Stock: " + expectedStock + ", Actual Stock: " + actualStock);
System.out.println("최근 주문 ["
+ "상품명: " + service.getLatestOrder(productName).getProductName()
+ ", 상품수: " + service.getLatestOrder(productName).getAmount() + " 건]");

// 재고 일치 여부 확인
assertEquals(actualStock, expectedStock);
}
}
16 changes: 16 additions & 0 deletions week0/OrderInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hanghae.radis.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class OrderInfo {
private String productName;
private Integer amount;
private Long timestamp;
}