nerdn
nerdn

Reputation: 329

Can objects instantiated within a static method be overwritten if the method is called on multiple threads?

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);
}

Upvotes: 0

Views: 631

Answers (1)

John Saunders
John Saunders

Reputation: 161831

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.

Upvotes: 6

Related Questions