Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A service hangs during startup. A thread dump shows thread T1 executing the static initializer of class A and blocked on class B's initialisation, while T2 executes B's static initializer and is blocked on A. Explain the mechanism and how to fix it.

Each class has an initialisation lock: a thread that needs a class another thread is initialising waits until that thread finishes. Here A's static initialiser needs B and B's needs A, so T1 holds A's initialisation and waits for B while T2 holds B's and waits for A, and both wait forever. The generic deadlock detector usually does not report it, and the fix is to break the static dependency cycle, move work out of static initialisers, or use the lazy holder idiom.

Why this question is asked

Start-up hangs are among the most frustrating production incidents: no exception, no error log, just a process that never becomes ready. Class initialisation deadlocks are a classic cause, and they tend to appear only after something harmless changes, such as a framework starting beans in parallel. Interviewers ask this because it tests whether we understand what the JVM does while a static initialiser runs. In this answer, we will recap how class initialisation is synchronised, reproduce the deadlock, see how it looks in a thread dump, go through the fixes, and write a test that reproduces it reliably.

1A Quick Recap of Initialisation Locking

A class is initialised once, the first time it is actively used, by running its static initialiser: its static blocks and static field initialisers. Since many threads might use a class at the same moment, the JVM has to make sure only one of them runs the initialiser and the others see the finished result.

It does this with a per-class initialisation lock and a state: not initialised, being initialised by thread X, initialised, or failed. A thread that finds the class being initialised by another thread waits until that thread finishes. A thread that finds the class being initialised by itself, a recursive request, simply continues, which is why a class can refer to its own static fields during initialisation. The waiting is invisible in our code: there is no synchronized keyword anywhere.

2Reproducing the Deadlock

Here are two classes whose static initialisers use each other, started from two threads at the same time.

Java
class A {
static final int VALUE;
static {
sleep(100); // widen the window so the threads overlap
VALUE = B.VALUE + 1; // needs B initialised
}
}
class B {
static final int VALUE;
static {
sleep(100);
VALUE = A.VALUE + 1; // needs A initialised
}
}
Thread t1 = new Thread(() -> System.out.println(A.VALUE), "T1");
Thread t2 = new Thread(() -> System.out.println(B.VALUE), "T2");
t1.start(); t2.start();
t1.join(); t2.join(); // never returns
Step throughTwo threads, two classes, one cycle
T1Class AClass BT2
start A's static init (A: in progress by T1)
start B's static init (B: in progress by T2)
A's init reads B.VALUE
B in progress by T2: T1 waits
B's init reads A.VALUE
A in progress by T1: T2 waits

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

Both threads wait on a class the other is initialising. No exception is ever thrown.

T1 starts initialising A and marks A as being initialised by T1. T2 starts initialising B and marks B as being initialised by T2. After the sleep, T1 reaches B.VALUE; B is being initialised by another thread, so T1 waits. T2 reaches A.VALUE; A is being initialised by T1, so T2 waits. Neither initialiser can finish, so neither thread is ever woken. Notice that if only one thread had started, it would have initialised A, then recursively B, then read A's partly initialised VALUE of 0, and finished without hanging; the cycle is a latent bug that two threads turn into a hang.

3What the Thread Dump Shows

A thread dump, taken with jcmd <pid> Thread.print or jstack, is the main evidence. What it shows depends on the JDK version. On older JDKs, the waiting threads often appear as RUNNABLE, sitting in the static initialiser frame (shown as <clinit>), with no lock named, which is confusing because nothing seems to be blocking them. Newer JDKs describe the wait more clearly, for example as waiting on the class initialisation monitor for a named class; check your version.

Text
"T1" #21 prio=5 os_prio=31 ... java.lang.Thread.State: RUNNABLE
at A.<clinit>(Main.java:6) // A's static init, waiting for B
at Main.lambda$main$0(Main.java:20)
"T2" #22 prio=5 os_prio=31 ... java.lang.Thread.State: RUNNABLE
at B.<clinit>(Main.java:13) // B's static init, waiting for A
at Main.lambda$main$1(Main.java:21)

The tell is two or more threads each stuck in a different class's <clinit> frame, each at a line that touches the other class. The generic deadlock detection at the end of a thread dump, which looks for cycles of monitors and java.util.concurrent locks, usually does not report this cycle, because class initialisation locks are not ordinary monitors. So we must read the stacks ourselves rather than trust the absence of a Found one Java-level deadlock line.

4Fixing It

The fundamental fix is to remove the cycle between the static initialisers. Often one value does not need the other at all, or a shared constant can move into a third class that depends on neither. A common real-world form of this bug is a superclass whose static field creates an instance of its own subclass, such as static final Shape DEFAULT = new Circle(); in Shape, because initialising Circle requires Shape to be initialised first; moving DEFAULT into a separate holder class breaks that cycle.

The second fix is to move work out of static initialisers into explicit initialisation methods called from start-up code in a known order, or into Spring beans whose dependencies the container orders for us. The third is the lazy holder idiom, which defers the dependent value until it is first needed, outside the outer class's initialisation.

Java
class A {
static int value() { return Holder.VALUE; }
private static final class Holder { // initialised on first call to value()
static final int VALUE = B.base() + 1;
}
}

Holder is initialised only when value() is first called, which is no longer during A's own static initialisation, so the lock on A is not held while B is being touched. The holder idiom is safe because class initialisation is itself thread safe, which is the property that caused the deadlock in the first place.

5Why It Appears After a Harmless Change

These cycles often exist for years without trouble, because a single start-up thread initialises everything in one order. They turn into hangs when start-up becomes concurrent: a framework starts beans in parallel, an eager background thread such as a cache warmer or metrics reporter begins while the main thread is still starting, or a new library touches a class from its own thread. The fix in the code is the same, but it is worth remembering that the change that exposed the bug is usually not the change that caused it.

6Follow-up questions

How would you reproduce this reliably in a test?

Make the timing deterministic instead of relying on sleeps. Start two threads that wait on a CyclicBarrier or CountDownLatch and then touch A and B at the same moment, and slow down each static initialiser a little, for example with a hook that waits on a second latch that both threads have reached. Run the test with a timeout, such as JUnit's assertTimeoutPreemptively, so a deadlock fails the test instead of hanging the build, and dump threads on timeout. Because class initialisation happens once per class loader, run each attempt in a fresh class loader or a fresh JVM fork, otherwise the second attempt finds the classes already initialised.

Why does the single-threaded version return a wrong value instead of hanging?

Because a recursive request from the thread already initialising a class does not wait. T1 initialising A reaches B, initialises B, and B reads A.VALUE while A is still in progress, so it sees the default 0. B.VALUE becomes 1 and A.VALUE becomes 2. The program finishes, but B saw A half-built, which is its own quiet bug.

Can enums cause the same problem?

Yes. Enum constants are created in the enum's static initialiser, so an enum whose constructors or static fields use another enum or class that uses it back can deadlock the same way when two threads touch them first at the same moment.

7Conclusion

In this answer, we saw that the JVM guards each class's initialisation with a lock and makes other threads wait while one thread runs the static initialiser. A cycle where A's static init uses B and B's uses A, started from two threads, leaves each thread waiting for the other forever, and the thread dump shows them in different <clinit> frames, often as RUNNABLE and without a reported deadlock. We fix it by removing the cycle, moving work into explicit initialisation, or using a lazy holder, and we test it with latches, timeouts and fresh class loaders.

What to say in the interviewAbout 90 seconds aloud

Open with

Every class has an initialisation state and lock. When a thread starts running a class's static initialiser, the class is marked as being initialised by that thread, and any other thread that needs the class waits until it finishes.

Then, if they want more

A recursive request from the same thread just continues. In this dump, T1 is running A's static initialiser, which touches B, but B is being initialised by T2, so T1 waits. T2 is running B's static initialiser, which touches A, which is being initialised by T1, so T2 waits. Neither initialiser can finish, so they wait forever, with no exception.

In the thread dump these threads are each in a <clinit> frame of a different class, and depending on the JDK version they can show as RUNNABLE or as waiting on class initialisation. The generic deadlock detector usually does not report it, because these are not ordinary monitors, so you have to read the stacks.

To fix it I would break the static dependency cycle, for example move shared values to a third class, or move the work out of static initialisers into explicit init methods or Spring beans called in a known order, or use the lazy holder idiom so the dependent value is computed on first use instead of during the outer class's initialisation.

These cycles often sit unnoticed until start-up becomes parallel or a background thread starts early, which is what exposes them.

About this answer

Last updated
12 November 2026
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 "A service hangs during startup. A thread dump shows thread T1 executing the static...": https://scrivio-interview-library.netlify.app/java/class-loading/288a316660/