如果在多个线程上调用静态方法中实例化的对象,是否可以覆盖该方法?

发布于 2024-09-06 22:49:27 字数 742 浏览 8 评论 0原文

如果您根据提供给静态方法的参数检索静态方法中的实例变量,如果不同的调用者同时调用该静态方法,该实例变量是否有可能被踩踏? 我调用的方法定义如下,我想知道实例变量发票是否会被损坏......任何澄清将不胜感激!

public static void SendInvoiceReceipt(int invoiceId, string recipientEmailAddress)
{
    var invoice = ObjectFactory.GetInvoiceDAL().GetInvoiceByInvoiceId(invoiceId);

    var htmlBody = BuildHtmlInvoiceReceipt(invoice);
    var txtBody = BuildTextInvoiceReceipt(invoice);

    UtilitiesManager.Emails.EmailUtil.Send(SiteConfigUtilities.GetSMTPServer(),
            "[email protected]", recipientEmailAddress, String.Empty,
            "Payment Receipt", htmlBody, txtBody);
}

If you retrieve an instance variable within a static method based on a parameter supplied to the static method, is it possible the instance variable can get stepped on if the static method is called at exactly the same time by different callers?
The method I am calling is defined below and I am wondering if the instance variable invoice can be corrupted... any clarification would be greatly appreciated!

public static void SendInvoiceReceipt(int invoiceId, string recipientEmailAddress)
{
    var invoice = ObjectFactory.GetInvoiceDAL().GetInvoiceByInvoiceId(invoiceId);

    var htmlBody = BuildHtmlInvoiceReceipt(invoice);
    var txtBody = BuildTextInvoiceReceipt(invoice);

    UtilitiesManager.Emails.EmailUtil.Send(SiteConfigUtilities.GetSMTPServer(),
            "[email protected]", recipientEmailAddress, String.Empty,
            "Payment Receipt", htmlBody, txtBody);
}

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

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

发布评论

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

评论(1

紫轩蝶泪 2024-09-13 22:49:27

invoice 是一个局部变量(不是“实例变量”)。它是在栈上分配的,每个线程都有自己的栈。另一个线程无法影响它。

invoice is a local variable (not an "instance variable"). It is allocated on the stack, and each thread has its own stack. There is no way for another thread to affect it.

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