WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Intermediate · question 25 of 100

Can you explain what the happens-before relationship is in Java?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

In a multi-threaded program, the order in which operations occur can greatly affect the outcome of the program. The happens-before relationship is a concept in Java that defines the ordering of memory operations between threads. Specifically, it is a relationship that ensures that memory updates made by one thread are visible to another thread.

In Java, the happens-before relationship is defined by several rules, which are used to determine when a write operation performed by one thread is guaranteed to be visible to another thread:

Program order rule - Within a single thread, each action occurs in program order.

int x = 1;
int y = 2;
int z = x + y;

Monitor lock rule - An unlock of a monitor happens-before every subsequent lock of that monitor.

synchronized (obj) {
    // do something
}

Volatile variable rule - A write to a volatile field happens-before every subsequent read of that field.

volatile int x = 1;

// Thread 1
x = 2;

// Thread 2
System.out.println(x); // will always print 2

Thread start rule - A call to Thread.start() on a thread happens-before any actions in the started thread.

Thread thread = new Thread(() -> {
    // do something
});
thread.start();

Thread join rule - All actions in a thread happen-before any other thread successfully returns from a join() on that thread.

Thread thread = new Thread(() -> {
    // do something
});
thread.start();
thread.join();
// thread is guaranteed to be finished at this point

Transitivity rule - If A happens-before B, and B happens-before C, then A happens-before C.

int x = 1;
synchronized (obj) {
    x = 2;
}
System.out.println(x); // will always print 2

By following these rules, we can ensure that the happens-before relationship is maintained between threads, and that memory operations are correctly synchronized. The happens-before relationship is important in Java because it provides a way to reason about the ordering of events in a multi-threaded program, and to ensure that the program behaves as expected.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics