问题描述
我有一些code我现在有一个静态类/方法,但我想检查,这将是线程安全的。从我读过,我认为这应该是确定的事,但在我的脑海里是说可能不是。我的网页的数据处理阶段使用外部Web服务创建订单记录,这可能是相当缓慢的:可能是30-40秒内,可能的话,5或10分钟(这是从我手中),所以我要火返回页面返回给用户,然后开始一个新的线程,然后邮件的用户一旦处理完成。这是目前在静态类/方法。只要我的所有对象的特定方法(除了系统默认值,这将是常见的),该方法应该是线程安全的,应该不会吧内创建。因此,举例来说,如果我有
I've got some code I currently have in a static class/method, but I wanted to check that it would be thread-safe. From what I've read I think this should be ok, but something in the back of my mind is saying it might not be. My web page's data processing stage uses an external web service to create order records, and this could be quite slow: possibly 30-40 seconds to, possibly, 5 or 10 minutes (this is out of my hands) so I was going to fire a return page back to the user, then start a new thread and then e-mail the user once the processing was complete. This is currently in a static class/method. Provided all my objects are created within the particular method (aside from system default values, which would be common) that method should be thread-safe, shouldn't it. So, for example, if I had
public static class ProcessOrder()
{
public static int GetOrderMaxSize()
{
return (....gets and parses ConfigurationManager.AppSettings["MaxOrderSize"]...);
}
public static bool CreateOrder(Order order)
{
XmlDocument xmlDoc = GetOrderXML(order);
bool check = false;
using (CreateOrderXML.Create xmlCo = new CreateOrderXML.Create())
{
xmlCo.Timeout = 60000;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
string xmlString = "";
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xmlDoc.WriteTo(xmlWriter);
xmlWriter.Flush();
xmlString = stringWriter.GetStringBuilder().ToString();
}
}
byte[] bXMLOrder = encoding.GetBytes(xmlString);
byte[] breturnMessage;
check = xmlCo.Create(bXMLOrder, out breturnMessage);
.... do something with return message
}
return check;
}
private static XmlDocument GetOrderXML(Order order)
{
... creates an XML object for the order
}
}
(该CreateOrderXML是一个服务引用到Web服务URL /法)那又是线程安全的,特别是对于长时间运行的(主要是在xmlCo.Create(....)级)并发线程?我明白,如果我开始把在课堂上的成员,然后用他们的方法,这肯定会推出不同主题覆盖值的问题,但只要对象中的方法创建的,他们应该是好的,不应该牛逼吧?
(the CreateOrderXML is a service reference to the Web Service URL/method) would that be thread-safe, especially for long-running (primarily at the xmlCo.Create(....) stage) concurrent threads? I understand that if I started putting in class members and then used them in the method, this would definitely introduce a problem with different threads overwriting the values, but so long as the objects are created within the methods, they should be ok, shouldn't they?
推荐答案
它看起来并不像你所访问的任何共享数据有;您请求远程资源,并建立了一套独特的数据,使用这种方法的每次执行。有没有必要进行同步那里。
It doesn't look like you are accessing any shared data there; you are requesting remote resources, and building a unique set of data with each execution of this method. There's no need for synchronization there.
这里所述方法的每次执行创造局部变量 - 它自己的副本。所以,没有什么是永远被共享。
Each execution of the method here is creating local variables - it's own copies. So nothing is ever being shared.
这篇关于保证线程安全上的静态方法在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!