Skip to content

사전과제#14

Open
hongs429 wants to merge 2 commits intohanghae-skillup:mainfrom
hongs429:사전과제

Hidden character warning

The head ref may contain hidden characters: "\uc0ac\uc804\uacfc\uc81c"
Open

사전과제#14
hongs429 wants to merge 2 commits intohanghae-skillup:mainfrom
hongs429:사전과제

Conversation

@hongs429
Copy link

@hongs429 hongs429 commented Jan 5, 2025

[사전 과제] 동시성 이슈 해결

[사전과제] 동시성 이슈 제어(Multi-thread 구조 only)



작업 내용

  • 단일 서버(Multi-Thread)에서 주문 요청에 대한 동시성을 ConcurrentHashMap 으로 해결
 // 상품 DB
private final Map<String, Integer> productDatabase = new ConcurrentHashMap<>();
  • 각 요청(Thread)별로 최신 상품이 무엇인지 보여주기 위해서, ThreadLocal로 해결
// 가장 최근 주문 정보를 저장하는 DB
private static final ThreadLocal<Map<String, OrderInformation>> latestOrderDatabase 
        = ThreadLocal.withInitial(ConcurrentHashMap::new);



발생했던 문제와 해결 과정을 남겨 주세요.

문제상황1
다수의 사용자가 1개의 app에서 동시에 같은 리소스를 업데이트할 때 재고 수량이 실제 구매된 갯수가 제대로 반영되지 않는 오류.

해결방안 및 이유
ConcurrentHashMap을 사용하여 내부적으로 동시성을 처리하고, 다중 스레드가 안전하게 Map에 접근할 수 있도록 설계

ConcurrentHashMap은 쓰기 작업 시 Map을 적절히 락으로 분할해 동시성 제어를 수행.

ConcurrentHashMap은 읽기 작업은 락 없이 동작하여 성능을 최적화.

문제상황2
각 요청(Thread) 별로 최신의 주문 데이터를 가져오지 못하는 오류.

해결방안 및 이유
ThreadLocal을 사용하여 다수의 Thread에서 접근하는 공유된 데이터를 Thread별로 독립적으로 데이터를 저장하고 관리

- 상품 데이터 : 동시성 발생 이슈 해결을 위해 ConcurrentHashMap 사용
- 최신 구매 상품 내역 : 각 Thread 별로 관리되어야 하므로, ThreadLocal 사용
- 위의 두가지 케이스에 대한 검증 TC 작성
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant