C# 基于Sleep的线程生成方法仿真
我试图理解.Net中的线程概念。 我无法使用Yield()方法。当我被10整除时,我希望控件转到并行线程 请帮忙 下面是我的示例代码:C# 基于Sleep的线程生成方法仿真,c#,.net,multithreading,C#,.net,Multithreading,我试图理解.Net中的线程概念。 我无法使用Yield()方法。当我被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 = ne
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 ");
}
}
}
类线程测试
{
//索引i被声明为静态的,因此两个线程只有一个副本
静态int-i;
静态void Main(字符串[]参数)
{
线程t=新线程(WriteY);
i=0;
//开始线程Y
t、 Start();
//在主线程上执行一些操作。
对于(;i<100;i++)
{
如果(i%10==0)
{
//模拟Yield()函数
睡眠(0);
Console.WriteLine(“X线程”);
}
控制台。写入(i+“:X”);
}
Console.ReadKey(true);
}
静态void WriteY()
{
对于(;i<100;i++)
{
如果(i%10==0)
{
//模拟Yield()函数
睡眠(0);
Console.WriteLine(“Y线程”);
}
控制台。写入(i+“:Y”);
}
}
}
我得到编译时错误:
System.Threading.Thread不包含“屈服”的定义
由都铎回答。此方法仅适用于.Net 4.0及以上版本
理想情况下,我希望启动一个线程,并希望每个线程执行10个递增的I。用我目前的方法,我要么得到所有的“X”要么得到所有的“Y”
编辑:
通过Tudor和TheHe-I的输入,我已经能够得到交替的X和Y。问题的关键是锁对象的使用。但这段代码的输出是不可预测的。在我看来,您正在当前线程中锁定“锁定器”,并希望将当前任务交给其他线程。。。 锁一直被第一根线锁着——它不能工作
如果要使用多个线程,必须手动锁定对象…在我看来,您正在当前线程中锁定“锁定器”,并希望将当前任务交给其他线程。。。 锁一直被第一根线锁着——它不能工作
如果要使用多个线程,则必须手动锁定对象…
线程。Yield
只需让计划程序选择另一个准备运行的线程即可:
使调用线程将执行交给另一个
准备在当前处理器上运行。操作系统选择
要屈服的线
如果应用程序中的其他线程也在等待该锁,那么您可以得到所有您想要的,它们将没有机会运行
顺便说一句,
Yield
是一种.NET4.0+方法。确保您的目标不是早期版本
编辑:在我看来,要想做你想做的事情,你应该使用事件:
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++;
}
}
}
类测试
{
//索引i被声明为静态的,因此两个线程只有一个副本
静态int-i;
static AutoResetEvent parentEvent=新的AutoResetEvent(true);
static AutoResetEvent childEvent=新的AutoResetEvent(false);
静态void Main(字符串[]参数)
{
线程t=新线程(WriteY);
i=0;
//开始线程Y
t、 Start();
//在主线程上打印X
parentEvent.WaitOne();
而(i<100)
{
如果(i%10==0)
{
childEvent.Set();
parentEvent.WaitOne();
}
控制台。写入(i+“:Y”);
i++;
}
t、 Join();
}
静态void WriteY()
{
childEvent.WaitOne();
而(i<100)
{
如果(i%10==0)
{
parentEvent.Set();
childEvent.WaitOne();
}
控制台。写入(i+“:X”);
i++;
}
}
}
线程。Yield
将使计划程序能够选择一个准备运行的不同线程:
使调用线程将执行交给另一个
准备在当前处理器上运行。操作系统选择
要屈服的线
如果应用程序中的其他线程也在等待该锁,那么您可以得到所有您想要的,它们将没有机会运行
顺便说一句,
Yield
是一种.NET4.0+方法。确保您的目标不是早期版本
编辑:在我看来,要想做你想做的事情,你应该使用事件:
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++;
}
}
}
类测试
{
//索引i被声明为静态的,因此两个线程只有一个副本
静态int-i;
static AutoResetEvent parentEvent=新的AutoResetEvent(true);
static AutoResetEvent childEvent=新的AutoResetEvent(false);
静态void Main(字符串[]参数)
{
线程t=新线程(WriteY);
i=0;
//开始线程Y
t、 Start();
//在主线程上打印X
parentEvent.WaitOne();
而(i<100)
{
如果(i%10==0)
{
childEvent.Set();
parentEvent.WaitOne();
}
控制台。写入(i+“:Y”);
i++;
}
t、 Join();
}
静态void WriteY()
{
childEvent.WaitOne();
而(i<100)
{
如果(i%10==0)
{
parentEvent.Set();
childEvent.WaitOne();
}
控制台。写入(i+“:X”);
i++;
}
}
}
忘记线程。产量
;这与你想做的事情无关。最后,您有一个锁
,它使用监视器
同步访问。在锁中
,线程以独占方式访问。您需要做的是暂时放弃锁;你这样做的方式是