What is synchronization and why is it used in Java
๐ก Concept: Synchronization in Java
Synchronization is a mechanism to control concurrent access to shared resources to ensure thread safety.
๐ Quick Intro
It prevents race conditions by allowing only one thread at a time to execute a synchronized block or method.
๐ง Analogy
Think of synchronization as a single key to a room; only one person can enter at a time to avoid conflicts.
๐ง Technical Explanation
- Synchronized methods or blocks lock the object or class to prevent simultaneous access.
- Helps maintain data consistency and avoid thread interference.
- Can synchronize instance methods, static methods, or code blocks.
- Locking granularity affects performance and concurrency.
- Java uses intrinsic locks (monitors) for synchronization.
๐ฏ Use Cases
- โ Protect shared mutable data.
- โ Coordinate access to critical sections.
- โ Prevent race conditions in multi-threaded programs.
๐ป Java Synchronization Example
public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}

โ Interview Q&A
Q1: What is synchronization in Java?
A: Controlling access to shared resources to avoid conflicts.
Q2: How does synchronization prevent race conditions?
A: By allowing only one thread at a time in critical section.
Q3: What can be synchronized?
A: Methods or code blocks.
Q4: What is intrinsic lock?
A: Monitor lock associated with each object.
Q5: Can static methods be synchronized?
A: Yes, they lock on the class object.
Q6: What happens if synchronization is missing?
A: Race conditions and inconsistent data.
Q7: What is lock granularity?
A: Size of code locked at once.
Q8: Can synchronization cause deadlock?
A: Yes, if locks are not managed properly.
Q9: Difference between synchronized and volatile?
A: Synchronized controls access; volatile ensures visibility.
Q10: What is synchronized block?
A: A block of code synchronized on a given object.
๐ MCQs
Q1. What is synchronization in Java?
- Parallel execution
- Controlling access to shared resources
- Memory management
- Garbage collection
Q2. How does synchronization prevent race conditions?
- Allows all threads
- Allows only one thread in critical section
- Ignores race
- Runs sequentially
Q3. What can be synchronized?
- Only methods
- Only blocks
- Methods or blocks
- None
Q4. What is intrinsic lock?
- External lock
- Monitor lock on object
- Thread lock
- Memory lock
Q5. Can static methods be synchronized?
- No
- Yes
- Sometimes
- Only for instance methods
Q6. What happens if synchronization is missing?
- Deadlock
- Race conditions
- Faster execution
- No effect
Q7. What is lock granularity?
- Thread size
- Size of locked code
- Memory size
- Process size
Q8. Can synchronization cause deadlock?
- No
- Yes
- Sometimes
- Never
Q9. Difference between synchronized and volatile?
- Same
- Controls access vs ensures visibility
- Only for primitives
- None
Q10. What is synchronized block?
- Method
- Code block synchronized on object
- Class
- Interface
๐ก Bonus Insight
Synchronization is crucial for thread safety but should be used judiciously to avoid performance bottlenecks.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!