如何编写正确的静态方法 - 多线程安全

发布于 2024-08-17 06:38:22 字数 958 浏览 6 评论 0原文

因为我认为静态方法不应该像第一个片段那样编写,还是我错了?

public static class ExtensionClass
{
 private static SomeClass object1;
 private static StringBuilder sb;

 private static string DoSomething()
 {
    sb.AppendLine(object1.SomeValue);
 }

 public static string ExtensionMethod(this HtmlHelper helper, SomeClass _object1)
 {
    object1 = _object1;
    sb = new StringBuilder();

    DoSomething();

    return sb.ToString();
 }
}

所以我想出了这个:

public static class ExtensionClass
{
  private static string DoSomething(ref StringBuilder _sb,SomeClass object1)
  {
    _sb.AppendLine(object1.SomeValue);
  }

  public static string ExtensionMethod(this HtmlHelper helper, SomeClass _object1)
  {
    SomeClass object1 = _object1;
    StringBuilder sb = new StringBuilder();

    DoSomething(ref sb,_object1);

    return sb.ToString();
  }

}

最后一个片段多线程安全吗?这应该是一个扩展方法,所以它不能是非静态的。或者是否有更好的方法在静态方法中传递非静态对象?

As I assume static methods shouldn't be writen like the first snippet , or am I wrong ?

public static class ExtensionClass
{
 private static SomeClass object1;
 private static StringBuilder sb;

 private static string DoSomething()
 {
    sb.AppendLine(object1.SomeValue);
 }

 public static string ExtensionMethod(this HtmlHelper helper, SomeClass _object1)
 {
    object1 = _object1;
    sb = new StringBuilder();

    DoSomething();

    return sb.ToString();
 }
}

So I come up with this:

public static class ExtensionClass
{
  private static string DoSomething(ref StringBuilder _sb,SomeClass object1)
  {
    _sb.AppendLine(object1.SomeValue);
  }

  public static string ExtensionMethod(this HtmlHelper helper, SomeClass _object1)
  {
    SomeClass object1 = _object1;
    StringBuilder sb = new StringBuilder();

    DoSomething(ref sb,_object1);

    return sb.ToString();
  }

}

Is this last snippet multithread safe ? This should be an extension method,so it cant be non-static. Or is there any better way of passing non-static object in a static method around ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

妄断弥空 2024-08-24 06:38:22

第二个片段与您传递给它的对象一样多线程安全。如果在 ExtensionMethod 运行时某些其他方法与 HtmlHelper 或 SomeClass 混在一起,那么您可能会遇到问题。但是 ExtensionMethod 有自己的 StringBuilder (与您的第一个代码片段不同),因此对 ExtensionMethod 的多次调用将获得不同的 StringBuilder,并且在这方面一切都会很好。

您是正确的,静态方法不应该像第一个片段那样编写。正如您所意识到的,如果线程 A 调用 ExtensionMethod,并且线程 B 调用 ExtensionMethod,而线程 A 仍在其中,则 sb 成员将更改为引用新的 StringBuilder。 A 迄今为止所做的所有工作都将丢失,并且 A 和 B 将从此追加到同一个 StringBuilder,从而产生不良结果!

The second snippet is as multithread-safe as the objects you're passing into it. If some other method mucks around with the HtmlHelper or the SomeClass while ExtensionMethod is running, then you may hit problems. But ExtensionMethod gets its own StringBuilder (unlike your first snippet) so multiple calls to ExtensionMethod will get different StringBuilders and all will be well on that front.

You are correct that static methods shouldn't be written like the first snippet. As you have realised, if Thread A calls ExtensionMethod, and Thread B calls ExtensionMethod while Thread A is still in there, the sb member will be changed to refer to a new StringBuilder. All the work that A has done so far will be lost, and A and B will henceforth be appending to the same StringBuilder, with undesirable results!

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