Please enable JavaScript.
Coggle requires JavaScript to display documents.
4.2 Instance Confinement - Coggle Diagram
4.2 Instance Confinement
Definition & Purpose
Use non-thread-safe objects safely
Confinement ensures access is restricted
Easier thread safety analysis
Types of Confinement
Class Instance Confinement
Private class members
Access confined to object methods
Lexical Scope Confinement
Local variables
Confined to method scope
Thread Confinement
Passed within a thread without sharing across threads
Advantages
Simplifies thread safety
Easier to reason about
Flexible locking strategies
Different locks for different state variables
Risks & Violations
Object Escape
Published references
Indirect publishing through iterators or inner classes
Scope Breach
Allowing access beyond intended confinement
Java Monitor Pattern
Encapsulates mutable state
Guards state with intrinsic lock
Simple and consistent
Can use private lock objects for better encapsulation
Private Lock Object vs. Intrinsic Lock
Private Lock Object
Accessibility: Private
Encapsulation: Stronger
Client Synchronization: Prevented
Liveness Issues: Reduced Risk
Intrinsic Lock
Accessibility: Public
Encapsulation: Weaker
Client Synchronization: Possible
Liveness Issues: Higher Risk