在多线程应用程序中使用类变量是不是很糟糕?

时间:2022-03-13 21:03:18

I inherited a project that needs to be mutithreaded. There is three major classes that get used in the worker threads.

我继承了一个需要多线程的项目。在工作线程中使用了三个主要类。

BASE CLASS - has an class level SqlDataAdapter and DataTable. INHERITED CLASS ONE - Uses the inherited SqlDataAdapter and DataTable. INHERITED CLASS TWO - Uses the inherited SqlDataAdapter and DataTable.

BASE CLASS - 具有类级别SqlDataAdapter和DataTable。 INHERITED CLASS ONE - 使用继承的SqlDataAdapter和DataTable。 INHERITED CLASS TWO - 使用继承的SqlDataAdapter和DataTable。

Every thing seams to work, but I only have two users testing at the same time.

每个东西都可以工作,但我只有两个用户在同一时间进行测试。

Is having the SqlDataAdapter and DataTable be class level variables a bad idea?

让SqlDataAdapter和DataTable成为类级变量是一个坏主意吗?

Update Sorry it's a SqlDataAdapter not a SqlTableAdapter. The language is C#. The SqlDataAdapter and DataTable are from the System.Data.SqlClient namespace.

更新抱歉,这是一个SqlDataAdapter而不是SqlTableAdapter。语言是C#。 SqlDataAdapter和DataTable来自System.Data.SqlClient命名空间。

Here is some of the base class:

这是一些基类:

public abstract class BaseSync
{
    #region Variables
    internal SqlDataAdapter stageDataAdapter;
    internal DataTable stageDataTable;
    #endregion //Variables
}

Part Two

There is also a singleton utility class that all the derived classes use. I don't know if it will cause problems or not. It looks like this:

还有一个单例实用程序类,所有派生类都使用它。我不知道它是否会引起问题。它看起来像这样:

public class Utility
{ 
    private static readonly Utility _utility= new Utility();

    private Utility()
    { }

    public static Utility GetUtility()
    {
        return _utility;
    }

    public int GetAutoNumber(string tablename, string fieldname, string siteId)
    {
        string _tablename = tablename;
        string _fieldname = fieldname;
        ...
    }

    internal MissingInfo NormalizeRow(DataRow dataRow)
    {

        MissingInfo retVal = MissingInfo.None;

        //Num
        if (dataRow["Num"] == DBNull.Value)
        {
           retVal =MissingInfo.Num;
           dataRow["Num"] = 1;
        }
        ...
    }
}

4 个解决方案

#1


This depends on the access level of the objects. As long as they are not Static (Shared in VB.NET). You should be fine having them in the object, as long as each thread, has its own instance of the object.

这取决于对象的访问级别。只要它们不是静态的(在VB.NET*享)。只要每个线程都有自己的对象实例,就可以将它们放在对象中。

Where you come into interesting situations are with the static members that are shared across all instances.

您遇到有趣情况的地方是所有实例共享的静态成员。

So the long and short of it is that we would need to see the code.

所以它的长短是因为我们需要看代码。

#2


Having variables modified by different threads without syncronization is always a really bad idea.

在没有同步的情况下让不同线程修改变量总是一个非常糟糕的主意。

You don't mention if this is the case though. If you thread, you need to plan and check what you're doing.

你没有提到是否是这种情况。如果你是线程,你需要计划和检查你在做什么。

#3


The rule about variables is that the more places they could potentially change from, the more chances there are of race conditions, especially if the application evolves.

关于变量的规则是,它们可能发生变化的地方越多,竞争条件的可能性就越大,特别是在应用程序发展的情况下。

There isn't much informtion in your question so it is difficult to provide a specific answer. Class-level variables (if public) can often be treated like global variables and are thus accessible from everywhere, raising the risk of corruption.

您的问题没有太多信息,因此很难提供具体的答案。类级变量(如果是公共的)通常可以被视为全局变量,因此可以从任何地方访问,从而增加了腐败的风险。

A possible approach might be to hide these fields and at provide access via class-level functions. You can then do more things because you've created specific points of access to these variables. You would need to be careful to ensure that you are never giving applications a direct and mutable references to that object, which may require some rewriting, but it would make your program safer.

一种可能的方法可能是隐藏这些字段并通过类级函数提供访问。然后,您可以执行更多操作,因为您已创建了对这些变量的特定访问点。您需要小心确保您永远不会向应用程序提供对该对象的直接和可变引用,这可能需要一些重写,但它会使您的程序更安全。

#4


you should always consider doing synchronization when sharing non-constant objects in multi-thread, otherwise you'll end screwed up someday...

你应该总是考虑在多线程*享非常量对象时进行同步,否则你有一天会搞砸...

so, it's OK if you want to make it a class variable, but remember to make some lock mechanism for it.

所以,如果你想让它成为一个类变量,那就没关系,但记得为它做一些锁定机制。

#1


This depends on the access level of the objects. As long as they are not Static (Shared in VB.NET). You should be fine having them in the object, as long as each thread, has its own instance of the object.

这取决于对象的访问级别。只要它们不是静态的(在VB.NET*享)。只要每个线程都有自己的对象实例,就可以将它们放在对象中。

Where you come into interesting situations are with the static members that are shared across all instances.

您遇到有趣情况的地方是所有实例共享的静态成员。

So the long and short of it is that we would need to see the code.

所以它的长短是因为我们需要看代码。

#2


Having variables modified by different threads without syncronization is always a really bad idea.

在没有同步的情况下让不同线程修改变量总是一个非常糟糕的主意。

You don't mention if this is the case though. If you thread, you need to plan and check what you're doing.

你没有提到是否是这种情况。如果你是线程,你需要计划和检查你在做什么。

#3


The rule about variables is that the more places they could potentially change from, the more chances there are of race conditions, especially if the application evolves.

关于变量的规则是,它们可能发生变化的地方越多,竞争条件的可能性就越大,特别是在应用程序发展的情况下。

There isn't much informtion in your question so it is difficult to provide a specific answer. Class-level variables (if public) can often be treated like global variables and are thus accessible from everywhere, raising the risk of corruption.

您的问题没有太多信息,因此很难提供具体的答案。类级变量(如果是公共的)通常可以被视为全局变量,因此可以从任何地方访问,从而增加了腐败的风险。

A possible approach might be to hide these fields and at provide access via class-level functions. You can then do more things because you've created specific points of access to these variables. You would need to be careful to ensure that you are never giving applications a direct and mutable references to that object, which may require some rewriting, but it would make your program safer.

一种可能的方法可能是隐藏这些字段并通过类级函数提供访问。然后,您可以执行更多操作,因为您已创建了对这些变量的特定访问点。您需要小心确保您永远不会向应用程序提供对该对象的直接和可变引用,这可能需要一些重写,但它会使您的程序更安全。

#4


you should always consider doing synchronization when sharing non-constant objects in multi-thread, otherwise you'll end screwed up someday...

你应该总是考虑在多线程*享非常量对象时进行同步,否则你有一天会搞砸...

so, it's OK if you want to make it a class variable, but remember to make some lock mechanism for it.

所以,如果你想让它成为一个类变量,那就没关系,但记得为它做一些锁定机制。