什么样的价值是的ThreadState物业?是的、物业、价值、ThreadState

2023-09-04 01:11:26 作者:男优报刊

This问题让我联想到了.NET相同。有没有给Thread类的ThreadState属性什么价值?在这种code例如:

This question got me thinking about the .NET equivalent. What value is there to the ThreadState property of the Thread class? In this code example:

        if (someThread.ThreadState != System.Threading.ThreadState.Running)
        {
            someThread = new Thread(SomeMethod);
            someThread.Start();
        }

该someThread的的ThreadState属性可以切换到,如果和里面的code之间运行,如果是吧?

The someThread's ThreadState property could switch to Running between the if and the code inside the if, right?

推荐答案

的ThreadState是看好起初那些梦幻般的特性之一,但一旦你潜水深入的方式,在功能,你会发现,它几乎完全无用。

ThreadState is one of those fantastic properties that look promising at first, but once you dive deep into the way in which in functions, you find that it's almost totally useless.

使用的ThreadState的主要问题是枚举名称非常具有误导性。例如采取ThreadState.Runnning。这是一个非常糟糕的名字,因为它实际上并不表明线程运行。相反,它表示螺纹于在最近的过去某一时刻运行,并且可以或可以不仍可以运行。

The main problem with ThreadState is the enumeration names are very misleading. For instance take ThreadState.Runnning. This is a really bad name because it does not actually indicate the thread is running. Instead it indicates the thread was running at some point in the recent past and may or may not still be running.

这看似微不足道,但事实并非如此。这真的很容易采取列举的名字从字面上并产生非常好看的code。但是还有code读取,它往往基于有缺陷的逻辑。

This may seem trivial but it's not. It's really easy to take the names of the enumeration literally and produces really nice looking code. However as well as the code reads, it's often based on flawed logic.

我真的只使用此属性进行调试。

I really only use this property for debugging purposes.

该值可以在非常有限的套,你必须控制你正在寻找的线程其他机制方案中非常有用。如锁或WaitHandle的。但它通常最好使用同步的另一种形式不是依靠此属性。

This value can be useful in very limited sets of scenarios where you have some other mechanism which controls the thread you are looking at. Such as a lock or WaitHandle. But it's usually better to use another form of synchronization than relying on this property.