← Back to libraryQuestion 18 of 273
JavaIntermediate

Synchronization

📖 Detailed Explanation:

Synchronization is the mechanism to control access to shared resources in a multithreaded environment. A 'synchronized' block or method acquires a 'Monitor Lock' on an object, forcing other threads to wait until the lock is released. This ensures mutual exclusion but can impact performance due to contention.

🌍 Real-World Example:

A bathroom in a restaurant. Only one person can use it at a time (synchronized). Others wait outside for their turn. Once someone leaves and unlocks the door, the next person can enter.

🎯 Scenario-Based Interview Question:

What is a Deadlock? Provide a scenario and explain how you can detect and prevent it.