开发者

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

开发者 https://www.devze.com 2023-03-16 13:49 出处:网络
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 lookin

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

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.

The Parallel.Foreach will call a private method, which in turn will call static methods from another assembly;

//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. Once complete, the MainMethod will send the processed WorkItem off to a Workflow System.

We will be processing these in batches of up to 30 in order not to overload the other systems. My concerns are basically the potential of 30 instances of WorkItem accessing the same static methods could cause some data integrity issues. 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.Da开发者_Python百科ta

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. 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?

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. ;>

Since the methods are static, they don't have any shared object instance to worry about. 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). Reading of global state is less of a concern, writing is what will throw a wrench in the works.

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. 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. If not then you're golden.

0

精彩评论

暂无评论...
验证码 换一张
取 消