简体   繁体   English

寻找有关使用 static 方法“处理” class 实例的线程安全的建议

[英]Looking for advice on thread safety using static methods to 'process' a class instance

I have recently inherited a system that uses a very basic approach to processing workitems, basically, it does them one by one.我最近继承了一个系统,它使用一种非常基本的方法来处理工作项,基本上,它一个一个地处理它们。 To be honest, up until recently this worked well.老实说,直到最近这还不错。 However, we are looking to implement a similiar process for another type of workitem and I have been looking into Task Parallel Library and think that will fit the bill.但是,我们正在寻求为另一种类型的工作项实施类似的流程,我一直在研究任务并行库,并认为这符合要求。 However, I have some concerns about Thread Safety and to be honest, this is an area that I lack knowledge, so I am asking only my 2nd question on here in hope that someone can give me some good points as I have yet to find a definitive yes or no answer for this.但是,我对线程安全有些担忧,老实说,这是我缺乏知识的领域,所以我在这里只问我的第二个问题,希望有人能给我一些好处,因为我还没有找到对此的明确是或否答案。

So we have our 'WorkItem' class所以我们有我们的'WorkItem' class

public class WorkItem
{
    public int Id {get; set;}
    public string data { get; set;}
}

A List<WorkItem> will be generated and these will then be processed using a Parallel.Foreach loop.将生成一个List<WorkItem> ,然后使用Parallel.Foreach循环处理这些。

The Parallel.Foreach will call a private method, which in turn will call static methods from another assembly; Parallel.Foreach将调用一个私有方法,该方法又会从另一个程序集调用 static 方法;

//Windows service that will run the Parallel.Foreach
private int MainMethod(WorkItem item) 
{
    item.Data = Processor.ProcessWorkItemDataProcess1(item.data);
    item.Data = Processor.ProcessWorkItemDataProcess2(item.data);
    SendToWorkFlow(item);
}


public static class Processor
{
    public static string ProcessWorkItemDataProcess1(string data)
    {
    //Process it here
    return string
    }

    public static string ProcessWorkItemDataProcess2(string data)
    {
        //Process it here
        return string
    }
}

And so on.等等。 All of these methods have logic in them to process the WorkItem instance at various different stages.所有这些方法都包含在各个不同阶段处理 WorkItem 实例的逻辑。 Once complete, the MainMethod will send the processed WorkItem off to a Workflow System.完成后, MainMethod会将处理后的 WorkItem 发送到 Workflow System。

We will be processing these in batches of up to 30 in order not to overload the other systems.我们将分批处理这些,最多 30 个,以免其他系统过载。 My concerns are basically the potential of 30 instances of WorkItem accessing the same static methods could cause some data integrity issues.我的担心基本上是 30 个WorkItem实例访问相同的 static 方法可能会导致一些数据完整性问题。 For example, ProcessWorkItemDataProcess2 is called with WorkItem1.Data and is subsequently called with WorkItem2.Data and somehow WorkItem2.Data is returned when it should be WorkItem1.Data例如,使用WorkItem1.Data调用ProcessWorkItemDataProcess2并随后使用WorkItem2.Data调用,并且当它应该是WorkItem2.Data时以某种方式返回WorkItem1.Data

All of the static methods are self-contained in so far as they have defined logic and will only (in theory) use the WorkItem that it was called with.所有 static 方法都是自包含的,只要它们定义了逻辑并且只会(理论上)使用调用它的WorkItem There are no methods such as DB access, file access, etc.没有数据库访问、文件访问等方法。

So, hopefully that explains what I am doing.所以,希望这能解释我在做什么。 Should I have any concerns?我应该有什么顾虑吗? If so, will creating an instance of the Processor class for each WorkItem solve any potential problems?如果是这样,为每个 WorkItem 创建一个Processor class 的实例会解决任何潜在问题吗?

Thanks in advance提前致谢

The scenario you describe doesn't sound like it has any blatant threading issues.您描述的场景听起来没有任何明显的线程问题。 Your worries about a static method being called on two different threads and getting the data mixed up is unfounded, unless you write code to mix things up.您担心在两个不同的线程上调用 static 方法并将数据混淆是没有根据的,除非您编写代码来混淆。 ;> ;>

Since the methods are static, they don't have any shared object instance to worry about.由于这些方法是 static,因此它们不需要担心任何共享的 object 实例。 That's good.那挺好的。 You have isolated the work into self-contained work items.您已将工作隔离为独立的工作项。 That is good.那很好。

You will need to check to make sure that none of the static methods access any global state, like static variables or properties, or reading from a file (the same file name for multiple work items).您需要检查以确保没有 static 方法访问任何全局 state,例如 static 变量或属性,或从文件中读取多个工作项(相同的文件名)。 Reading of global state is less of a concern, writing is what will throw a wrench in the works.全局 state 的读取不那么令人担忧,而写入才是工作中的关键。

You should also review your code to see how data is assigned to your work items and whether any of the code that processes the work items modifies the work item data.您还应该查看您的代码,以了解如何将数据分配给您的工作项,以及处理工作项的任何代码是否修改了工作项数据。 If the work items are treated as strictly read only by the methods, that's good.如果工作项被方法严格地视为只读,那很好。 If the methods write changes back to fields or properties of the work items, you will need to double check that the data in the work items is not shared with any other work items.如果方法将更改写回工作项的字段或属性,您将需要仔细检查工作项中的数据是否未与任何其他工作项共享。 If the code that constructs the work item instances assigns a cached value to a property of multiple work items, and the static methods modify properties of that value, you will have threading conflicts.如果构造工作项实例的代码将缓存值分配给多个工作项的属性,并且 static 方法修改该值的属性,您将遇到线程冲突。 If the work item construction always constructs new instances of values that are assigned to properties of the work item, this shouldn't be an issue.如果工作项构造总是构造分配给工作项属性的值的新实例,那么这应该不是问题。

In a nutshell, if you have multiple threads accessing shared state, and at least one is writing, then you need to worry about thread safety.简而言之,如果您有多个线程访问共享 state,并且至少有一个正在写入,那么您需要担心线程安全。 If not then you're golden.如果没有,那么你就是金子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM