JVM 동시성 처리
💡synchronized
1)
- synchronized를 쓰면 동시에 여러 스레드가 접근하는 상황(Thread interference) 을 막는다.
- 메모리 일관성 문제(memory consistency) 도 예방한다.
Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors.
2)
- synchronized는 모니터 락(intrinsic lock) 을 사용하는 동기화 메커니즘이다.
- 한 객체에 대해 한 번에 한 스레드만 락을 획득할 수 있다.
Each object is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor.
3)
- synchronized 메서드/블록은 이 모니터 락을 획득 → 코드 실행 → 자동 해제 한다.
- JVM은 락을 자동으로 획득/해제한다(프로그램이 직접 unlock 호출 X)
💡Reentrant Lock
1)
- 기본 동작은 synchronized와 같다(상호 배제).
- 그러나 기능이 더 많고 유연하다.
A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities.
2) 재진입성 (Reentrant)
- 같은 스레드가 같은 락을 여러 번 얻을 수 있다 → 재진입 가능(Reentrant)
A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it… The method will return immediately if the current thread already owns the lock.
- 예시
lock.lock();
methodA();
lock.unlock();
➡️ methodA 내부에서 또 lock.lock()을 호출 가능
3) 공정성 설정 (fair)
- new ReentrantLock(true) → 오래 기다린 스레드 먼저!
- 반면 synchronized는 기본적으로 공정성 보장을 제공하지 않음
The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread.
4) Memory Sync
- ReentrantLock 역시 JVM의 synchronized와 동일한 메모리 가시성 보장(happens-before) 을 제공한다.
All Lock implementations must enforce the same memory synchronization semantics as provided by the built-in monitor lock …
비교
💡synchronized
💡Reentrant Lock
참고 자료
1) 블로그 : [JAVA] synchronized VS Reentrant Lock (차이점)
https://velog.io/@may_yun/JAVA-synchronized-VS-Reentrant-Lock-차이점
[JAVA] synchronized VS Reentrant Lock (차이점)
상호 배제를 통한 동기화의 개념을 선행하고 보면 이해하는데 도움이 됩니다.현재 데이터를 사용하고 있는 해당 스레드를 제외하고 나머지 스레드들은 데이터에 접근 할 수 없도록 막는 개념Jav
velog.io
2) Oracle Docs : Synchronized
https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
Synchronized Methods (The Java™ Tutorials > Essential Java Classes > Concurrency)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Dev.java for updated tutorials taking advantag
docs.oracle.com
3) Oracle Docs : ReentrantLock
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html
ReentrantLock (Java Platform SE 8 )
Acquires the lock only if it is not held by another thread at the time of invocation. Acquires the lock if it is not held by another thread and returns immediately with the value true, setting the lock hold count to one. Even when this lock has been set to
docs.oracle.com
'Projects > HubEleven' 카테고리의 다른 글
| [동시성 처리] Trouble Shooting : 멀티스레드 재고 감소 통합 테스트 코드 Eureka Server Connection refused (0) | 2026.02.16 |
|---|---|
| [동시성 처리] synchronized vs Reentrant Lock - 실습 (0) | 2026.02.15 |
| [동시성 처리] Trouble Shooting : 테스트 코드 .env 환경 변수 적용 (0) | 2026.02.13 |
| [동시성 처리] Trouble Shooting : 테스트 코드 Config Client 오류 (0) | 2026.02.06 |
| [동시성 처리] 동시성 문제 (Race Condition) 해결 방법 3가지 관점 (실습중..) (0) | 2026.02.05 |