ASP.NET代码隐藏类中的静态方法是否是非线程安全的?

时间:2022-02-07 15:36:54

Can I use static methods in my ASP.NET Pages and UserControls classes if they don't use any instance members? I.e.:

如果不使用任何实例成员,我可以在ASP.NET Pages和UserControls类中使用静态方法吗?即:

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gridStatement.DataSource = CreateDataSource();
    gridStatement.PageIndex = e.NewPageIndex;
    gridStatement.DataBind();
}

private static DataTable CreateDataSource()
{
    using (var command = new SqlCommand("SELECT foobar"))
    {
        var table = new DataTable();
        new SqlDataAdapter(command).Fill(table);
        return table;
    }
}

Or this is not thread-safe?

或者这不是线程安全的?

3 个解决方案

#1


8  

Yes, you can use static members - they are thread-safe. Each thread will execute in a separate context and therefore any objects created inside a static method will only belong to that thread.

是的,您可以使用静态成员 - 它们是线程安全的。每个线程将在单独的上下文中执行,因此在静态方法内创建的任何对象将只属于该线程。

You only need to worry if a static method is accessing a static field, such as a list. But in your example the code is definitely thread-safe.

如果静态方法正在访问静态字段(例如列表),则只需要担心。但在您的示例中,代码绝对是线程安全的。

#2


2  

nothing shared across threads, so it is thread safe. unless you access static members that other static methods have a chance of executing concurrently with it...

没有在线程之间共享,所以它是线程安全的。除非你访问静态成员,否则其他静态方法有可能与它同时执行...

#3


1  

it is. The only thing to worry about in your context about thread-safeness is a concept that involves static members, as already said. When any method (static or not) accesses a static member, you should worry about multithreading issues. Consider the following:

它是。在您的上下文中,关于线程安全性唯一要担心的是涉及静态成员的概念,如前所述。当任何方法(静态或非静态)访问静态成员时,您应该担心多线程问题。考虑以下:

public class RaceConditionSample
{
    private static int number = 0;
    public static int Addition()
    {
        int x = RaceConditionSample.number;
        x = x + 1;
        RaceConditionSample.number = x;
        return RaceConditionSample.number;
    }

    public int Set()
    {
        RaceConditionSample.number = 42;
        return RaceConditionSample.number;
    }

    public int Reset()
    {
        RaceConditionSample.number = 0;
        return RaceConditionSample.number;
    }
}

RaceConditionSample sample = new RaceConditionSample();
System.Diagostics.Debug.WriteLine(sample.Set());

// Consider the following two lines are called in different threads in any order, Waht will be the
// output in either order and/or with any "interweaving" of the individual instructions...?
System.Diagostics.Debug.WriteLine(RaceConditionSample.Addition());
System.Diagostics.Debug.WriteLine(sample.Reset());

The answer is: It may be "42, 43, 0", "42, 0, 1" you wont know before..

答案是:它可能是“42,43,0”,“42,0,1”,你以前不知道..

#1


8  

Yes, you can use static members - they are thread-safe. Each thread will execute in a separate context and therefore any objects created inside a static method will only belong to that thread.

是的,您可以使用静态成员 - 它们是线程安全的。每个线程将在单独的上下文中执行,因此在静态方法内创建的任何对象将只属于该线程。

You only need to worry if a static method is accessing a static field, such as a list. But in your example the code is definitely thread-safe.

如果静态方法正在访问静态字段(例如列表),则只需要担心。但在您的示例中,代码绝对是线程安全的。

#2


2  

nothing shared across threads, so it is thread safe. unless you access static members that other static methods have a chance of executing concurrently with it...

没有在线程之间共享,所以它是线程安全的。除非你访问静态成员,否则其他静态方法有可能与它同时执行...

#3


1  

it is. The only thing to worry about in your context about thread-safeness is a concept that involves static members, as already said. When any method (static or not) accesses a static member, you should worry about multithreading issues. Consider the following:

它是。在您的上下文中,关于线程安全性唯一要担心的是涉及静态成员的概念,如前所述。当任何方法(静态或非静态)访问静态成员时,您应该担心多线程问题。考虑以下:

public class RaceConditionSample
{
    private static int number = 0;
    public static int Addition()
    {
        int x = RaceConditionSample.number;
        x = x + 1;
        RaceConditionSample.number = x;
        return RaceConditionSample.number;
    }

    public int Set()
    {
        RaceConditionSample.number = 42;
        return RaceConditionSample.number;
    }

    public int Reset()
    {
        RaceConditionSample.number = 0;
        return RaceConditionSample.number;
    }
}

RaceConditionSample sample = new RaceConditionSample();
System.Diagostics.Debug.WriteLine(sample.Set());

// Consider the following two lines are called in different threads in any order, Waht will be the
// output in either order and/or with any "interweaving" of the individual instructions...?
System.Diagostics.Debug.WriteLine(RaceConditionSample.Addition());
System.Diagostics.Debug.WriteLine(sample.Reset());

The answer is: It may be "42, 43, 0", "42, 0, 1" you wont know before..

答案是:它可能是“42,43,0”,“42,0,1”,你以前不知道..