【发布时间】:2016-09-06 08:03:40
【问题描述】:
我有两个方法,first() 和 second(),我将它们分配给两个线程。
以下是我的第二种和第一种方法
public void first()
{
lock (this)
{
Monitor.Wait(this);
Console.WriteLine("First");
Monitor.Pulse(this);
}
}
public void second()
{
lock (this)
{
//Monitor.Wait(this);
Console.WriteLine("Second");
Monitor.Pulse(this);
}
}
问题出在控制台上,只有“Second”被打印出来。 即使我在 second() 中有 Monitor.Pulse() 来通知控件应该切换到 first() 但这并没有发生。
我关注msdn关注https://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
如果s 线程没有在 second() 中使用Monitor.Pulse() 将控制权转移到f 线程,我们将不胜感激
以下是我的 Main()
test t = new test();
test1 t1 = new test1();
Thread f = new Thread(new ThreadStart(t.first));
Thread s = new Thread(new ThreadStart(t1.second));
f.Start();
s.Start();
f.Join();
s.Join();
【问题讨论】:
标签: c# .net multithreading thread-synchronization