是静态方法是线程安全的 [英] Are static methods thread safe

查看:113
本文介绍了是静态方法是线程安全的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态Timer类将由任何网页被调用,以计算出每个页面已经采取了多长时间来构建。

I have a static timer class which will be called by ANY webpage to calculate how long each page has taken to be constructed.

我的问题是在静态类线程安全的?在我的例子会并发用户造成问题,我开始和结束时间?例如一个不同的线程覆盖我的启动和停止值。

My question is are Static classes thread safe? In my example will concurrent users cause a problem with my start and stop times? e.g a different threads overwriting my start and stop values.

public static class Timer
{
    private static DateTime _startTime;
    private static DateTime _stopTime;    

    /// <summary>
    /// Gets the amount of time taken in milliseconds
    /// </summary>
    /// <returns></returns>
    public static decimal Duration()
    {
        TimeSpan duration =  _stopTime - _startTime;
        return duration.Milliseconds;
    }

    public static void Start()
    {
        _startTime = DateTime.Now;
    }

    public static void Stop()
    {
        _stopTime = DateTime.Now;
    }
}

如果这个类是一个非静态类?

Should this class be a non-static class?

(这个类将被称为从asp.net母版。)

(This class will called from the asp.net masterpage.)

推荐答案

静态方法是不是的本身的线程安全的。他们由CLR比实例方法对待。不同的是,一个人应该普遍尝试的的他们线程安全的。 (我想不出任何.NET BCL静态方法这是不是线程安全的。)实例方法,因为典型的模式是创建一个对象,并从一个线程重复使用它往往不是线程安全的,如果它确实的已被使用从多个线程,所涉及的协调包括确保该对象的安全使用。在很多情况下,这更合适的统筹code做多的对象本身。 (通常你要进行的操作有效原子的全序列 - 东西不能在对象中进行。)

Static methods aren't inherently thread-safe. They're treated no differently by the CLR than instance methods. The difference is that one should generally try to make them thread-safe. (I can't think of any .NET BCL static methods which aren't thread-safe.) Instance methods are often not thread-safe because the typical pattern is to create an object and use it repeatedly from one thread, and if it does have to be used from multiple threads, the co-ordination involved includes making sure that the object is used safely. In very many cases that's more appropriate to do in the co-ordinating code than in the object itself. (Usually you want to make whole sequences of operations effectively atomic - something which can't be done within the object.)

定时类是最肯定不是线程安全的:两个线程可以踩对方的轻松的数据,并没有什么,从使用停止线程陈旧数据计算时间时。

Your Timer class is most definitely not thread-safe: two threads can stomp on each other's data with ease, and there's nothing to stop a thread from using "stale" data when calculating the duration.

使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx\"><$c$c>Stopwatch类,而不是 - 这就是它的存在了。诚然,如果你想使用多线程一个实例,你需要采取正常的步骤,以确保安全,但你会在总体上更有利的地位。诚然秒表远非完美太 - 看到这个问题和以下评论更多细节 - 但它至少是个什么类型的设计。 (谁知道,它可能是固定的一段时间......)

Use the Stopwatch class instead - that's what it's there for. Admittedly if you want to use one instance from multiple threads you'll need to take the normal steps to ensure safety, but you'll be in a much better position in general. Admittedly Stopwatch is far from perfect too - see this question and the comment below for more details - but it is at least what the type is designed for. (Who knows, it may be fixed some time...)

这篇关于是静态方法是线程安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆