简体   繁体   中英

What is object publishing and why do we need it?

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 .

Publishing the Object is basically nothing but creating an instance of an Object and hence that object is available to use by the respective reference. Normally it is referred in the sense that objects should be safely published which means that no one should be able to see that object in an inconsistent state. ie it should not be available for use till it is not completely constructed.

But in definition point of view it is nothing but instantiating an object which again means the object is published

"Object publishing" is not an established technical term, certainly not in the Java ecosystem. It seems to be part of the name of Zope , a Python web framework.

My guess is that your interviewers either use this term within their company and are not aware of this, or the question was meant to filter out people who cannot admit that there is something they don't know and will instead make up stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM