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!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: