English 中文(简体)
IS/asp.net中的静态方法和电线
原标题:static methods and the call stack in IIS/asp.net
  • 时间:2009-10-09 12:03:20
  •  标签:

Theoretical question. If you have 100 separate requests coming to an aspx web page that calls the static method below.

    public static GeocodeResult GeocodeQuery(string query)
    {
        int train, tube, dlr = 0;

        // manipulate these ints
        if (train)
        {
            // do something important
        }

    }

每一项请求是否都有单独的呼吁。

If so - Does this static method call get pushed on these separate call stacks?

If so - Therefore are these ints thread safe? ie. 100 requests, 100 call frames, 300 ints.

卡车

问题回答

是的,每项请求都有自己的呼吁。 由于当地变量和方法学参数储存在堆积中,因此,每组将包含query、培训、管道、dir自己的数值。

因此,除非你们能够进入某种共同国家,否则这种方法就变得安全。

如果固定方法的唯一变量是当地变量或参数,那么所有固定方法都是安全的。

请注意,这种方法不带有警钟,透镜有警钟。 方法由一线read子使用。

除非采用这种方法,否则这种方法可能会把参数推向点火,但肯定会把返回地址推向点。

每一条read线都独立于任何其他线索的警钟。 如果只有 st,那么这种方法就变得read。

还可以使用其他储存,使这种方法不会变得read。 这包括静态数据和实例数据以及这些数据中提到的物体。 静态方法无法查阅案例数据,因此更容易阅读。 仅需要观察静态数据或静态数据的物体的获取情况:

private static int _balance;
private static void ThreadSafetyIssues()
{
    // Not thread  safe
    int temp = _balance;
    temp ++;
    _balance = temp;

    // Not thread safe
    temp = (int) HttpContext.Current.Session["balance"];
    temp ++;
    HttpContext.Current.Session["balance"] = temp;
}

The HttpContext.Current property isagna. 这意味着任何校对可同时查阅该背景,包括<代码>><>>>。 如同上面简单的静态<代码>_balance领域一样,这同样是非读物。





相关问题
热门标签