平等比较线程线程、平等

由网友(我深知我该努力了)分享简介:我要寻找一种方式来解释,这是不合理的洒高层次的商业逻辑和调用的ReferenceEquals()。下面是一个code片段,我有(precondition的一种方法,旨在抛出,如果我们是在一个错误的线程)一个问题:如果(!object.ReferenceEquals(Thread.CurrentThread,Reques...

我要寻找一种方式来解释,这是不合理的洒高层次的商业逻辑和调用的ReferenceEquals()。

下面是一个code片段,我有(precondition的一种方法,旨在抛出,如果我们是在一个错误的线程)一个问题:

 如果(!object.ReferenceEquals(Thread.CurrentThread,RequestHandlerThread))
 

时的它,而不是写这个可靠的:

 如果(Thread.CurrentThread!= RequestHandlerThread)
 
Linux进程与线程的区别 详细总结 面试经验总结

我建议使用ManagedThreadIds在基于我通常看到的教程比较。对手说,比较,以供参考平等似乎更面向对象的。

下面的(大致)我在System.Object的反射器的观点看到了与.NET 4.0。请记住,Thread类是密封的,并且没有过载的==操作符。

 公共静态布尔的ReferenceEquals(对象objA,对象objB)
{
    返回(objA == objB);
}

公共静态布尔等于(对象objA,对象objB)
{
    返回(objA == objB ||
        (objA = NULL和放大器;!&安培; objB = NULL和放大器;!&安培; objA.Equals(objB)));
}
 

下面是一些基本的测试,验证对线程池的操作......我错过任何显著测试?

 使用的System.Threading;
使用System.Diagnostics程序;
使用System.Threading.Tasks;

命名空间ConsoleApplicationX
{
   类节目
   {
      静态只读主题mainThread;

      静态程序()
      {
         mainThread = Thread.CurrentThread;
      }

      静态无效的主要(字串[] args)
      {
         线程线程= Thread.CurrentThread;
         如果(线程!= Thread.CurrentThread)
            Debug.Fail();

         如果(Thread.CurrentThread!=螺纹)
            Debug.Fail();

         如果(线程!= mainThread)
            Debug.Fail();

         VAR任务= Task.Factory.StartNew(()=> RunOnBackground(螺纹));
         task.Wait();

         VAR anotherThread =新主题(新ParameterizedThreadStart(RunInAnotherThread));
         anotherThread.Start(螺纹);
      }

      静态无效RunOnBackground(螺纹fromInitial)
      {
         如果(Thread.CurrentThread == fromInitial)
            Debug.Fail();

         如果(fromInitial!= mainThread)
            Debug.Fail();
      }

      静态无效RunInAnotherThread(对象fromInitialAsObject)
      {
         VAR fromInitial =(线程)fromInitialAsObject;

         如果(Thread.CurrentThread == fromInitial)
            Debug.Fail();

         如果(fromInitial!= mainThread)
            Debug.Fail();
      }
   }
}
 

解决方案

简短的回答:

使用ManagedThreadId属性进行比较。

简单的例子:

想象一下,我们有一类称为StackOverflow.MessageThread。程序员胖手指precondition一个函数,说Debug.Assert的(Thread.CurrentThread == MessageThread)。 precondition在运行时失败。如果开发将达到为ManagedThreadId,他会发现在编辑时,它是不可能的,而且会解决先前在开发周期的问题。

I am looking for a way to explain that it's unreasonable to sprinkle high-level business logic with calls to ReferenceEquals().

Here's a code snippet that I have a problem with (precondition in a method, designed to throw if we're on a wrong thread):

if (!object.ReferenceEquals(Thread.CurrentThread, RequestHandlerThread))

Is it reliable to write this instead:

if (Thread.CurrentThread != RequestHandlerThread)

I suggested to use ManagedThreadIds in the comparison based on what I commonly see in tutorials. Adversary says that comparison for reference equality seems more object-oriented.

Here's (roughly) what I saw in Reflector's view of System.Object with .NET 4.0. Keep in mind that Thread class is sealed, and has no overload for operator==.

public static bool ReferenceEquals(object objA, object objB)
{
    return (objA == objB);
}

public static bool Equals(object objA, object objB)
{
    return (objA == objB || 
        (objA != null && objB != null && objA.Equals(objB)));
}

Here are some basic tests, verifying operation on thread pool... Did I miss any significant tests?

using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleApplicationX
{
   class Program
   {
      static readonly Thread mainThread;

      static Program()
      {
         mainThread = Thread.CurrentThread;
      }

      static void Main(string[] args)
      {
         Thread thread = Thread.CurrentThread;
         if (thread != Thread.CurrentThread)
            Debug.Fail("");

         if(Thread.CurrentThread != thread)
            Debug.Fail("");

         if (thread != mainThread)
            Debug.Fail("");

         var task = Task.Factory.StartNew(() => RunOnBackground(thread));
         task.Wait();

         var anotherThread = new Thread(new ParameterizedThreadStart(RunInAnotherThread));
         anotherThread.Start(thread);
      }

      static void RunOnBackground(Thread fromInitial)
      {
         if (Thread.CurrentThread == fromInitial)
            Debug.Fail("");

         if (fromInitial != mainThread)
            Debug.Fail("");
      }

      static void RunInAnotherThread(object fromInitialAsObject)
      {
         var fromInitial = (Thread)fromInitialAsObject;

         if (Thread.CurrentThread == fromInitial)
            Debug.Fail("");

         if (fromInitial != mainThread)
            Debug.Fail("");
      }
   }
}

解决方案

Short answer:

Use ManagedThreadId property for comparisons.

Simple Example:

Imagine we have a class called StackOverflow.MessageThread. Programmer fat-fingers a precondition for a function, saying Debug.Assert(Thread.CurrentThread == MessageThread). Precondition fails at runtime. If the dev would reach for ManagedThreadId, he'd find out at edit-time that it's impossible, and would fix the problem earlier in the development cycle.

阅读全文

相关推荐

最新文章