Medium Programming & OOP Concurrency & Threading
Explain the Java Memory Model and volatile keyword.
Answer
JMM defines how threads interact through memory. Each thread may have local cache; without synchronization, threads may see stale values. volatile guarantees: visibility (writes immediately visible to all threads), prevents reordering around volatile access. Not atomic for compound operations (i++). Happens-before: establishes ordering (unlock before lock, volatile write before read, thread start/join). Use volatile for: flags, status variables, simple communication. Use synchronized/locks for: compound operations, multiple related variables. volatile + CAS enables lock-free algorithms.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Java Developer Backend Developer Systems Developer