Java multi-threading Safe Publication
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Safe publication in Java means making an object visible to other threads in a way that guarantees they observe a fully constructed and correctly initialized state. Without safe publication, another thread may see stale values, partially initialized fields, or state that appears impossible from a single-threaded perspective.
This is a Java Memory Model issue, not just a style preference. The object may be constructed correctly, but if it is published incorrectly, other threads are not guaranteed to see that construction.
What Unsafe Publication Looks Like
The classic bug is storing an object into a shared field without synchronization, then reading it from another thread.
This looks harmless, but without a happens-before relationship, another thread is not guaranteed to observe the write safely.
Common Safe Publication Mechanisms
Java gives several reliable ways to publish safely:
- storing the reference in a
finalfield during construction - publishing through a
volatilefield - publishing under the same lock used for reading
- publishing from a static initializer
- publishing through thread-safe containers such as
ConcurrentHashMap
Each of these creates the visibility guarantees that plain unsynchronized sharing lacks.
Example with volatile
If a reference is written to a volatile field, later readers of that field see the initialized object state.
This is a common pattern for configuration reloads or lazily initialized shared state.
Final Fields Help, but Publication Still Matters
final fields have special visibility guarantees after correct construction. That is why immutable objects are so attractive in concurrent code.
However, immutability does not give you permission to publish the reference through a broken sharing mechanism. The publication step still must be safe.
Safe Publication Through Initialization or Locks
Static initialization is inherently safe because class initialization has the right memory barriers.
Locks also work because unlocking in one thread and locking in another creates a happens-before relationship.
This is slower than immutable one-time publication in some scenarios, but it is correct and easy to reason about.
Common Pitfalls
- Assuming object construction alone makes the object safe to share across threads.
- Confusing thread-safe mutation with safe publication of the initial reference.
- Relying on immutable fields but then publishing the object through an unsynchronized shared variable.
- Using double-checked locking without
volatileon the shared reference. - Fixing visibility for writers but not for readers, which leaves no full happens-before chain.
Summary
- Safe publication means other threads are guaranteed to see a fully initialized object.
- Plain unsynchronized sharing does not provide that guarantee.
- Reliable publication mechanisms include
final,volatile, synchronization, static initialization, and thread-safe containers. - Immutable objects are easier to publish safely, but the publication step still matters.
- Think in terms of happens-before relationships, not just object construction order.
Related reading
- Java Multithreading for IVRS with GSM Modem rxtx playing voice file making event listener stop working
- Java Non-Blocking and Asynchronous IO with NIO NIO.2 JSR203 - Reactor/Proactor Implementations
- Java notify() vs. notifyAll() all over again
- Java notify vs. notifyAll all over again
- Java Multiple class declarations in one file
- Java Multiple Inheritance
- Java .parallelStream with spring annotated methods
- Java program that calls external program behaving asynchronously?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.