During one of my job interviews for Java Developer I was asked a question:
What is object publishing and why do we need it?
And I'm not sure I know the right answer:
I think that object publishing is is when the object (variable) state is put to heap memory. And it's needed for objects (variables) sharing between threads.
Am I right? Please correct me if I'm wrong. I've been searching the Java Language Specification, but found nothing that would unambiguously answer this question.
Hint: The question is deliberately formed in non strict and exact way - that's the catch of it.
Your answer was pretty close. I would define object publication as the act by one thread of making a reference to an object visible to another thread. Usually, this is connected with object creation: you need to ensure that a newly-created object is published in such a way that the other thread will see it in an initialised state.
The classic text on this (and many other thread-related matters) is Java Concurrency in Practice. If you don't have a copy of that, and can't be bothered to google up a pirated PDF, not that i would suggest you do that, and indeed even if you do or can, you should heed the mighty CERT Oracle Secure Coding Standard for Java's advice that you Do not publish partially initialized objects.