静态方法导致问题
我有一个静态方法如下:
public static void writeArticle(TypeA typeA, TypeB typeB) {
AWriter writer = AFactory.getWriter("aWriter");
Article article = writer.newArticle();
/* PARAMETER WRITE START */
article.set("title", typeA.getTitle());
article.set("author", typeB.getName());
article.set("age", typeB.getAge());
// …
/* more set statments here */
writer.write(article);
}
这个方法会导致作者写一篇价值混合的文章吗?也就是说,当 2 个类(A 类和 B 类)实例调用此方法时,Article 是否会从 ClassA 获取一些 typeA 值,从 ClassB 获取一些 typeA 值?
I have a static method as following:
public static void writeArticle(TypeA typeA, TypeB typeB) {
AWriter writer = AFactory.getWriter("aWriter");
Article article = writer.newArticle();
/* PARAMETER WRITE START */
article.set("title", typeA.getTitle());
article.set("author", typeB.getName());
article.set("age", typeB.getAge());
// …
/* more set statments here */
writer.write(article);
}
Could this method cause a problem that the writer will write a value-mixed Article? That is, when 2 class (Class A and ClassB) instances calling this method, will Article get some of typeA values from ClassA and some from ClassB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不。您认为为什么两个不同电话的论点会混淆?没有理由认为他们会这么做。
如果这是一个多线程程序,那么您当然应该小心线程之间共享对象;如果这些对象具有可变状态(可以更改的成员变量),您应该注意两个线程不要同时修改状态。
No. Why do you think the arguments from two different calls would get mixed up? There's no reason to think they would.
If this is a multi-threaded program, you should ofcourse be careful with sharing objects between threads; if those objects have mutable state (member variables that can be changed) you should take care that two threads are not modifying the state at the same time.
每当您说
static
时,您并不是在谈论实例方法调用。应始终使用静态方法的类名来调用。静态方法在实例级别是完全隔离的。即使它可能会写出一个混合值,但这就是你想要它写的,不是吗?
Whenever you say
static
, you do not talk about instance method calls. Always static methods should be called with their class name.The static method is totally isolated at instance level. Even though it might write a value-mixed, it is what you want it to write isnt it?
不,方法参数和局部变量是线程安全的只要它们不引用共享对象。
No, method parameters and local variables are thread-safe as long as they do not refer to shared objects.
不,否则那就是地狱了。
No. It would be hell otherwise.
仅当两个调用都访问并更改公共状态时才会出现这种情况,例如,假设定义此方法的类称为
ArticleWriter
,它们都会读取并更改ArticleWriter< 的静态字段/代码>。
This would only be the case if both calls accessed and changed common state, e.g. assuming the class in which this method is defined is called
ArticleWriter
, they would both read and change static fields ofArticleWriter
.这对于你的代码来说是不可能的。
typeA
有方法getTitle
,typeB
有方法getName
。哪些领域正在混淆?That would be impossible for your code.
typeA
has methodgetTitle
andtypeB
has methodgetName
. Which fields are getting mixed up?从这段代码片段中无法得知。首先要解决的问题是该方法是否可以同时从两个不同的线程调用。如果是这样,问题很可能是两个线程同时修改同一个对象(文章或作者)。解决这个问题的最简单方法是同步 writeArticle()。
It's unknowable from this code snippet. The first question to address is whether or not this method may be called from two different threads at the same time. If so the problem is most likely that two threads are modifying the same object at the same time (either article or writer). The easiest way to address that would be to synchronize writeArticle().