这个是线程的wait和 pulse使用例子。我在调试的时候想到一个问题请各位达人给一个解释谢谢了
具体提问见代码里的注释
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace WaitandPulse
{
class WaitAndPulse
{
static void Main(string[] args)
{
LockME l = new LockME(); WaitPulse e1 = new WaitPulse(l);
WaitPulse2 e2 = new WaitPulse2(l); Thread t1 = new Thread(new ThreadStart(e1.CriticalSection));
t1.Start(); Thread t2 = new Thread(new ThreadStart(e2.CriticalSection));
t2.Start(); Console.ReadKey();
}
public class LockME
{
} public class WaitPulse
{
private int result = 0;
private LockME _lM; public WaitPulse()
{
} public WaitPulse(LockME l)
{
this._lM = l;
} public void CriticalSection()
{
Monitor.Enter(this._lM); Console.WriteLine("WaitPulse:Enter Thread"+ Thread.CurrentThread.GetHashCode());
for (int i = 1; i <= 5;i++ )
{
Monitor.Wait(this._lM);
Console.WriteLine("WaitPulse:WokeUp");
Console.WriteLine("WaitPulse:Result=" +
result++ + " ThreadID "+
Thread.CurrentThread.GetHashCode());
Monitor.Pulse(this._lM);
}
Console.WriteLine("WaitPulse:Exiting Thread" + Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._lM);
}
} public class WaitPulse2
{
private int result = 0;
internal LockME _1M; public WaitPulse2()
{
} public WaitPulse2(LockME l)
{
this._1M = l;
} public void CriticalSection()
{
Monitor.Enter(this._1M);
Console.WriteLine("WaitPulse2:Entered Thread" +
Thread.CurrentThread.GetHashCode());
for (int i = 1; i <= 5; i++ )
{
Monitor.Pulse(this._1M); //线程首先去waitpulse然后等待,接着程序来到这里。在此处临时释放锁,若果
Console.WriteLine("WaitPulse2:Result = " + //这个pulse和下面的(红色的)之间的操作占用的时间片很长的话
result++ + " ThreadId " + //waitpulse 会不会获waipulse2上对lockme的锁
Thread.CurrentThread.GetHashCode()); Monitor.Wait(this._1M); Console.WriteLine("WaitPulse2:WokeUp");
} Console.WriteLine("WaitPulse2:Exiting Thread" +
Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._1M);
}
}
}
}
具体提问见代码里的注释
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace WaitandPulse
{
class WaitAndPulse
{
static void Main(string[] args)
{
LockME l = new LockME(); WaitPulse e1 = new WaitPulse(l);
WaitPulse2 e2 = new WaitPulse2(l); Thread t1 = new Thread(new ThreadStart(e1.CriticalSection));
t1.Start(); Thread t2 = new Thread(new ThreadStart(e2.CriticalSection));
t2.Start(); Console.ReadKey();
}
public class LockME
{
} public class WaitPulse
{
private int result = 0;
private LockME _lM; public WaitPulse()
{
} public WaitPulse(LockME l)
{
this._lM = l;
} public void CriticalSection()
{
Monitor.Enter(this._lM); Console.WriteLine("WaitPulse:Enter Thread"+ Thread.CurrentThread.GetHashCode());
for (int i = 1; i <= 5;i++ )
{
Monitor.Wait(this._lM);
Console.WriteLine("WaitPulse:WokeUp");
Console.WriteLine("WaitPulse:Result=" +
result++ + " ThreadID "+
Thread.CurrentThread.GetHashCode());
Monitor.Pulse(this._lM);
}
Console.WriteLine("WaitPulse:Exiting Thread" + Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._lM);
}
} public class WaitPulse2
{
private int result = 0;
internal LockME _1M; public WaitPulse2()
{
} public WaitPulse2(LockME l)
{
this._1M = l;
} public void CriticalSection()
{
Monitor.Enter(this._1M);
Console.WriteLine("WaitPulse2:Entered Thread" +
Thread.CurrentThread.GetHashCode());
for (int i = 1; i <= 5; i++ )
{
Monitor.Pulse(this._1M); //线程首先去waitpulse然后等待,接着程序来到这里。在此处临时释放锁,若果
Console.WriteLine("WaitPulse2:Result = " + //这个pulse和下面的(红色的)之间的操作占用的时间片很长的话
result++ + " ThreadId " + //waitpulse 会不会获waipulse2上对lockme的锁
Thread.CurrentThread.GetHashCode()); Monitor.Wait(this._1M); Console.WriteLine("WaitPulse2:WokeUp");
} Console.WriteLine("WaitPulse2:Exiting Thread" +
Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._1M);
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货