线程在 Nunit 中无法正常工作吗?

发布于 2024-07-15 11:58:21 字数 1396 浏览 2 评论 0原文

我认为当涉及线程代码时,nunit 无法正常运行:

这是示例代码:

public class multiply
{
    public Thread myThread;
    public int Counter
    {
        get;
        private set;
    }
    public string name
    {
        get;
        private set;
    }
    private Object thisLock = new Object();
    public void RunConsolePrint()
    {
        //lock(thisLock)
        //{
        Console.WriteLine("Now thread " + name + " has started");
        for (int i = 1; i<= Counter; i++)
        {
            Console.WriteLine(name + ": count has reached " + i+ ": total count is "+Counter);
        }
        Console.WriteLine("Thread " + name + " has finished");
        //}
    }
    public multiply(string pname, int pCounter)
    {
        name = pname;
        Counter = pCounter;
        myThread = new Thread(new ThreadStart(RunConsolePrint));
    }
}

这是测试代码:

[Test]
public  void Main()
{
    counter=100;

    multiply m2=new multiply("Second", counter);
    multiply m1 = new multiply("First", counter);
    m1.myThread.Start();
    m2.myThread.Start();

}

输出是 m1m2 的顺序运行>,这意味着 m1 中的循环总是先于 m2 执行,至少我的测试显示是这样。 我运行了几次测试,每次都得到这个结果。

这是一个错误吗? 还是预期的行为?

如果我将上述代码复制到控制台程序并运行,我可以清楚地看到线程效果。

我正在使用 TestDriven.net 运行器进行测试。

I think that nunit isn't function properly when threading code is involved:

Here's the sample code:

public class multiply
{
    public Thread myThread;
    public int Counter
    {
        get;
        private set;
    }
    public string name
    {
        get;
        private set;
    }
    private Object thisLock = new Object();
    public void RunConsolePrint()
    {
        //lock(thisLock)
        //{
        Console.WriteLine("Now thread " + name + " has started");
        for (int i = 1; i<= Counter; i++)
        {
            Console.WriteLine(name + ": count has reached " + i+ ": total count is "+Counter);
        }
        Console.WriteLine("Thread " + name + " has finished");
        //}
    }
    public multiply(string pname, int pCounter)
    {
        name = pname;
        Counter = pCounter;
        myThread = new Thread(new ThreadStart(RunConsolePrint));
    }
}

And here's the test code:

[Test]
public  void Main()
{
    counter=100;

    multiply m2=new multiply("Second", counter);
    multiply m1 = new multiply("First", counter);
    m1.myThread.Start();
    m2.myThread.Start();

}

And the output is a sequential running of m1 and m2, which means that the loop in m1 is always execute first before m2, at least that's what my testing shows. I ran the tests a few times and I always get this.

Is this a bug? Or an expected behavior?

If I copy the above code to a console program and run, I can see the threading effect clearly.

I am using the test using TestDriven.net runner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2024-07-22 11:58:21

两个或多个线程的确切交错实际上无法预测。

对于您的示例,需要考虑几个因素。 首先,每个线程将在(可能)切换到另一个线程之前执行其量程。 您不能期望线程切换发生在代码中的任何特殊位置。 即一个线程可能会在另一个线程启动之前完成(特别是因为在您的情况下任务相对较短)。

其次,由于您正在写入控制台,因此线程在该访问上是同步的。 这也会影响交错。

结果还取决于计算机上可用核心的数量(以及运行代码时计算机上的一般负载)。

简而言之,您无法预测两个线程将如何运行。

The exact interleaving of two or more threads cannot really be predicted.

There are a couple of factors to consider for your example. First of all each thread will execute for its quantum before (potentially) getting switched to another thread. You can't expect thread switches to happen on any special place in your code. I.e. one thread might finish before the other one is started (especially since the task is relatively short in your case).

Secondly, since you're writing to the console, the threads are synchronized on that access. This also affects the interleaving.

The result also depends on the number of available core on your machine (as well as the general load on the machine when you run the code).

In short, you cannot predict how the two threads will run.

杀お生予夺 2024-07-22 11:58:21

m1 还是 m2 首先开始执行是不确定的。 如果计数器执行得足够快,看到其中一个在另一个开始之前完成我不会感到惊讶。 将计数更改为非常大的值(例如一百万),我很确定您会看到单独的线程同时执行。

任何结果都是可能的 - 您看到什么结果表明存在错误?

It's non-deterministic whether m1 or m2 starts executing first. If the counter executes fast enough, I wouldn't be surprised to see one of them finish before the other starts. Change the count to something very large (e.g. a million) and I'm pretty sure you'll see separate threads executing concurrently.

Any result is possible - what result are you seeing that suggests there's a bug?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文