静态方法导致问题

发布于 2024-09-17 08:41:44 字数 531 浏览 3 评论 0原文

我有一个静态方法如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一口甜 2024-09-24 08:41:44

不。您认为为什么两个不同电话的论点会混淆?没有理由认为他们会这么做。

如果这是一个多线程程序,那么您当然应该小心线程之间共享对象;如果这些对象具有可变状态(可以更改的成员变量),您应该注意两个线程不要同时修改状态。

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.

哭泣的笑容 2024-09-24 08:41:44

每当您说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?

冰雪梦之恋 2024-09-24 08:41:44

不,方法参数和局部变量是线程安全的只要它们不引用共享对象

No, method parameters and local variables are thread-safe as long as they do not refer to shared objects.

初见 2024-09-24 08:41:44

不,否则那就是地狱了。

No. It would be hell otherwise.

失去的东西太少 2024-09-24 08:41:44

仅当两个调用都访问并更改公共状态时才会出现这种情况,例如,假设定义此方法的类称为 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 of ArticleWriter.

若水微香 2024-09-24 08:41:44

这对于你的代码来说是不可能的。 typeA 有方法 getTitletypeB 有方法 getName。哪些领域正在混淆?

That would be impossible for your code. typeA has method getTitle and typeB has method getName. Which fields are getting mixed up?

颜漓半夏 2024-09-24 08:41:44

从这段代码片段中无法得知。首先要解决的问题是该方法是否可以同时从两个不同的线程调用。如果是这样,问题很可能是两个线程同时修改同一个对象(文章或作者)。解决这个问题的最简单方法是同步 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文