c# - 使用 Sleep 模拟 Thread Yield 方法

标签 c# .net multithreading

我正在尝试了解 .Net 中的线程概念。 我无法使用 Yield() 方法。当 i 可以被 10 整除时,我希望控件转到并行线程。

请帮忙。

下面是我的示例代码:

class ThreadTest
{
    //Index i is declared as static so that both the threads have only one copy
    static int i;


    static void Main(string[] args)
    {
        Thread t = new Thread(WriteY);          
        i = 0;

        //Start thread Y    
        t.Start();                              
        //Do something on the main thread.
        for (; i < 100; i++)
        {
            if (i % 10 == 0)
            {
                //Simulate Yield() function
                Thread.Sleep(0);
                Console.WriteLine("The X thread");
            }
            Console.Write(i + ":X ");
        }
        Console.ReadKey(true);
    }

    static void WriteY()
    {
        for (; i < 100; i++)
        {
            if (i % 10 == 0)
            {
                //Simulate Yield() function
                Thread.Sleep(0);
                Console.WriteLine("The Y thread");
            }
            Console.Write(i + ":Y ");
        }
    }
}

我得到编译时错误:

System.Threading.Thread does not contain a definition for 'Yield'

由 Tudor 回答。此方法仅适用于 .Net 4.0 及更高版本。

理想情况下,我希望启动一个线程并希望每个线程执行 10 个递增的 i。使用我当前的方法,我要么得到所有“X”,要么得到所有“Y”。

编辑: 通过 Tudor 和 TheHe 的输入——我已经能够得到替代的 X 和 Y。问题的症结在于锁定对象的使用。但是这段代码的输出是不可预测的。

最佳答案

Thread.Yield 只会让调度程序选择一个准备好运行的不同线程:

Causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to.

如果您的应用程序中的其他线程也在等待该锁,您可以放弃所有您想要的,它们将没有机会运行。

顺便说一句,Yield 是一种 .NET 4.0+ 方法。确保您没有针对早期版本。

编辑:IMO,做你想做的事你应该使用事件:

class Test
{
    //Index i is declared as static so that both the threads have only one copy
    static int i;

    static AutoResetEvent parentEvent = new AutoResetEvent(true);
    static AutoResetEvent childEvent = new AutoResetEvent(false);

    static void Main(string[] args)
    {
        Thread t = new Thread(WriteY);

        i = 0;

        //Start thread Y
        t.Start();
        // Print X on the main thread
        parentEvent.WaitOne();
        while (i < 100)
        {                
            if (i % 10 == 0)
            {
                childEvent.Set();
                parentEvent.WaitOne();
            }
            Console.Write(i + ":Y ");
            i++;
        }
        t.Join();
    }

    static void WriteY()
    {
        childEvent.WaitOne();
        while (i < 100)
        {
            if (i % 10 == 0)
            {
                parentEvent.Set();
                childEvent.WaitOne();
            }
            Console.Write(i + ":X ");
            i++;
        }
    }
}

关于c# - 使用 Sleep 模拟 Thread Yield 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12420546/

相关文章:

c# - 使用 WPD 将文件复制到 Windows Phone C#

c# - NHibernate,我应该使用 ReadCommitted 还是 ReadUncommited 事务隔离级别?

c# - 处理 SQL Azure 数据库副本

python - 要求另一个线程做某事

c# - 在 Unity3D 中按名称加载脚本

c# - 如何正确处理子 Action 异常

c# - 创建 protected 链接

c - 我应该如何在不使用锁的情况下在另一个线程的回调中注册更新?

c++ - 从 std::thread 在主线程上执行回调函数

c# - C# 集合是否始终强制执行顺序?