什么是对象发布?为什么我们需要它? [英] What is object publishing and why do we need it?

查看:112
本文介绍了什么是对象发布?为什么我们需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我对Java Developer的一次面试中,我被问到一个问题:

During one of my job interviews for Java Developer I was asked a question:

什么是对象发布?我们为什么需要它?

我不确定我知道正确的答案:

And I'm not sure I know the right answer:

我认为那个对象发布是将对象(变量)状态放入堆内存时。并且线程之间共享对象(变量)是必需的。

我是对的吗?如果我错了,请纠正我。我一直在搜索Java语言规范,但没有发现任何可以毫不含糊地回答这个问题的内容。

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.

关于此问题的经典文本(以及许多其他与线程相关的事项)是 Java Concurrency in Practice 。如果你没有那个副本,并且不能打扰盗版PDF,那不是我建议你这样做,而且即使你做了或者可以,你应该留意强大的CERT Oracle Secure Java编写标准建议您不要发布部分初始化的对象

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.

这篇关于什么是对象发布?为什么我们需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆