ProgrammingBackend Developer

Describe how the synchronized keyword works in Java, in what cases it should be used, and what problems incorrect usage can lead to?

Pass interviews with Hintsage AI assistant

Answer.

The synchronized keyword has been present in Java since the support of multithreading, ensuring exclusive access to blocks of code or methods from different threads. Historically, this is Java's answer to the classic problems of race conditions and data inconsistency that arise when multiple threads access shared data simultaneously.

The problem with multithreading is ensuring the "atomicity" of operations and preventing conflicting state changes of objects. Improper use of synchronized can lead to deadlocks, performance degradation, or even a lack of synchronization due to incorrect choice of monitor object.

The solution is to use the synchronized keyword for a method or block of code where you need to guarantee that only one thread executes that section at a time. Careful selection of the synchronization object is essential, avoid long critical sections, and for more complex tasks, use special classes from the java.util.concurrent package.

Example code:

public class Counter { private int count = 0; public synchronized void increment() { count++; } public int getCount() { return count; } } // Synchronizing only part of the code on a separate object: private final Object lock = new Object(); public void complexIncrement() { synchronized (lock) { // critical section count++; } } }

Key features:

  • synchronized blocks access to the code for other threads by the chosen "monitor"
  • Methods can be synchronized entirely or only specific blocks
  • Synchronization is a powerful but risky tool (deadlock, performance)

Trick questions.

What is the difference between a synchronized method and a synchronized block?

A synchronized method blocks the object's (or class's if the method is static) "monitor", while a synchronized block allows you to explicitly specify a monitoring object, providing greater flexibility.

What happens if you synchronize different methods in one object?

If both methods are marked as synchronized (not static), both use the object itself (this) as the monitor. One thread will not be able to enter either method while another is in them.

Can you synchronize static methods and instance methods? How do they unlock relative to each other?

Static methods use the monitor of the class itself (Class object), while instance methods use the instance object. Therefore, a static synchronized method and an instance synchronized method do not block each other.

Common mistakes and anti-patterns

  • Synchronizing on the wrong object (e.g., on String or an object from a pool)
  • Too long or obscure critical sections leading to degradation
  • Circular waiting or deadlock
  • Misusing synchronized instead of using modern alternatives from java.util.concurrent

Real-life example

Negative case

A developer synchronizes methods across different objects but uses the same String as monitor everywhere. This results in slowdowns and "random" deadlocks due to string reuse from the pool.

Pros:

  • The code is short and written faster

Cons:

  • High risk of deadlocks
  • Difficult to debug
  • Severe performance drop

Positive case

Synchronization on a private final object that is created only within the class (private final Object lock), and critical sections are minimized in time.

Pros:

  • Guarantees correct behavior
  • Minimal overhead
  • Easy to read and maintain

Cons:

  • Requires discipline and experience
  • Not always obvious for novice developers