Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

Review these lock choices and explain the problem with each: (a)

Java
synchronized (lock) {
lock = new Object();
...
}

(b)

Java
private final Boolean flag = false;
synchronized (flag) { ... }

(c) synchronized ("orders") { ... }

(a) Reassigning the lock field inside the block means the next thread synchronizes on a different object, so two threads can be inside at once; lock fields must be final. (b) private final Boolean flag = false autoboxes to Boolean.FALSE, a single JVM-wide instance, so any unrelated code that locks a false Boolean shares our lock. (c) String literals are interned, so every "orders" literal in the JVM is the same object and any other code using it shares the lock. The fix in all three is a private final Object lock = new Object() owned by the class, and static analysis tools flag these patterns.

Why this question is asked

All three snippets compile and seem to work in tests, which is what makes them dangerous. Each one fails because of what the lock object really is: a field that changes, or an object that is secretly shared with the whole JVM. Interviewers use this question to check that we understand synchronized locks an object, not a variable or a name. In this answer, we will recap what the lock in synchronized (x) actually is, take each snippet in turn and show how it breaks, then look at the correct pattern and the tools that catch these mistakes.

1A Quick Recap: synchronized Locks an Object, Not a Variable

When a thread runs synchronized (x) { ... }, Java evaluates the expression x at that moment, takes the object it refers to, and acquires that object's monitor. The variable is only a way to reach the object. Two threads exclude each other only if they reach the same object.

So two questions decide whether a lock works. Does every thread always reach the same object? And is that object private to us, or can other code reach it too? Each snippet in the question fails one of these.

2Snippet (a): Reassigning the Lock Inside the Block

Here the lock field is changed while it is held. Thread A locks the original object and replaces the field. Thread B arrives, reads the field, gets the new object, and locks it without waiting, because nobody holds the new object's monitor. Now A and B are both inside the block.

Java
private Object lock = new Object(); // not final
void update() {
synchronized (lock) { // A locks object #1
lock = new Object(); // field now points to object #2
counter++; // B locks object #2 and runs this too
}
}
// two threads x 100,000 calls -> counter often below 200000
Step throughTwo threads, two different lock objects
Thread Alock fieldThread B
read lock -> object #1, acquire
assign object #2
read lock -> object #2, acquire
counter++
counter++ at the same time

5 messages in order. Play them, or step through them one at a time.

A holds #1, B holds #2: no mutual exclusion.

Notice that even without the reassignment inside the block, a non-final lock field is risky: any code that assigns it, and any thread that reads it before it is safely published, can end up with a different object. The rule is simple: lock fields are final, so the compiler guarantees they never change.

3Snippet (b): Locking a Boolean

The field is final, so it never changes, but the object it refers to is the problem. private final Boolean flag = false; is autoboxed with Boolean.valueOf(false), which returns the cached constant Boolean.FALSE. There is exactly one Boolean.FALSE in the JVM, so our lock is shared with every class, library and framework that ever synchronizes on a false Boolean.

That causes two kinds of trouble. Unrelated code can make our threads wait for no reason, and if that code also takes other locks, we can get a deadlock between two components that never knew about each other. Boxed integers have the same problem, since Integer.valueOf caches values from -128 to 127. Since Java 16 (JEP 390), the wrapper classes are documented as value-based, javac warns about synchronizing on them with -Xlint:synchronization, and the runtime flag -XX:DiagnoseSyncOnValueBasedClasses can log or fail when it happens.

Java
private final Boolean flag = false; // the same object as Boolean.FALSE
System.out.println(flag == Boolean.FALSE); // true
synchronized (flag) { ... } // shared with every other lock on Boolean.FALSE

4Snippet (c): Locking a String Literal

String literals are interned: the JVM keeps one String object for each distinct literal and gives the same object to every class that uses it. So synchronized ("orders") in our class and synchronized ("orders") in a completely different library lock the very same object.

The result is the same as with Boolean: unexpected waiting between unrelated parts of the program, and possible deadlocks that appear only when both parts run together in production. It is especially tempting with strings because the name looks meaningful, but a name is not ownership.

Java
Object a = "orders";
Object b = "orders"; // in another class or library
System.out.println(a == b); // true: one interned object
String c = new String("orders");
System.out.println(a == c); // false, but locking on c invites other mistakes

Notice that new String("orders") would produce a distinct object, but locking on strings at all is confusing, and an intern() call somewhere would bring the problem back. A plain Object says clearly that it exists only to be a lock.

5The Correct Pattern

All three problems disappear with a lock object that is final, so it never changes, and private, so no other code can reach it. Nothing else in the JVM refers to it, so nobody can lock it by accident, and the locking stays an internal detail of our class.

Java
class OrderBook {
private final Object lock = new Object(); // final: never changes; private: ours alone
private int count;
void add() {
synchronized (lock) { count++; } // 200000 after 2 threads x 100,000
}
int count() {
synchronized (lock) { return count; }
}
}

If we need one lock for the whole class rather than per instance, the same pattern works with private static final Object LOCK = new Object().

6Letting Tools Catch It

These mistakes are mechanical, so tools find them reliably. SpotBugs reports synchronization on a field that is changed inside the block and synchronization on Boolean, boxed primitives and shared constants such as interned strings (patterns in its DL_ and ML_ families; check your version for exact names). Sonar and Error Prone have similar rules, and javac's -Xlint:synchronization warns about value-based classes. Turning these on in the build means the review never has to depend on someone spotting them by eye.

7Follow-up questions

Is synchronizing on this also a problem?

It is legal and correct for internal state, but it exposes the lock: any caller holding a reference to our object can synchronize on it and block our methods, or take part in a deadlock. A private lock object avoids that, which is why it is preferred for library and shared classes.

Why does a lock need to be final if the field is assigned only once in the constructor?

Correctness then depends on nobody ever adding an assignment later, and on safe publication of the object. final makes the first guarantee a compiler rule and also gives the field the memory model's final-field visibility, so every thread is sure to see the initialised lock.

Can we lock on a Class object such as OrderBook.class?

Yes, and static synchronized methods do exactly that. But the Class object is public: any code can lock it. For a class-wide lock that only we control, a private static final Object is safer.

8Conclusion

In this answer, we saw that synchronized evaluates the expression and locks the object it refers to. Reassigning the lock field lets later threads lock a different object, so lock fields must be final. A Boolean false is the shared Boolean.FALSE and a string literal is an interned, JVM-wide object, so both share our lock with unrelated code and invite waiting and deadlocks. A private final Object owned by the class fixes all three, and SpotBugs, Sonar, Error Prone and javac's synchronization lint catch these patterns automatically.

What to say in the interviewAbout 80 seconds aloud

Open with

All three fail because synchronized locks an object, and here the object is either changing or shared with the whole JVM. In (a), the block reassigns the lock field.

Then, if they want more

Thread A holds the old object, then thread B reads the field, gets the new object and enters without waiting, so two threads are inside together. Lock fields have to be final. In (b), the field is final, but private final Boolean flag = false autoboxes to Boolean.FALSE, which is one shared instance in the JVM.

Any other code that synchronizes on a false Boolean, maybe in a library, shares our lock, so we get unexplained blocking and possible deadlocks. Since Java 16 the wrappers are value-based classes and javac warns about locking them. In (c), string literals are interned, so every "orders" literal anywhere in the JVM is the same object, with the same problem.

The fix for all three is a private final Object lock = new Object() owned by the class, or a private static final one for a class-wide lock: it never changes and no one else can reach it. I would also enable SpotBugs or Sonar rules and javac's synchronization lint, because these patterns are easy to detect automatically.

About this answer

Last updated
12 November 2026
Versions it names
Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Review these lock choices and explain the problem with each: (a)": https://scrivio-interview-library.netlify.app/java/synchronization/fa24e5f7c2/