public class LockMe //这个类有什么作用?
{
}
public class WaitPulse1
{
private int result = 0;
private LockMe _lm;
public WaitPulse1(LockMe l)
{
this._lm = l; //_lm是LockMe的对象,this._lm中this应该是WaitPulse1吧?这种表示如何理解?
}
public void CriticalSection()
{
Monitor.Enter(this._lm); //此处只写成this不行吗?为什么?
Console.WriteLine("waitpulse1:enter thread" + Thread.CurrentThread.GetHashCode());
for (int i = 1; i < 5; i++)
{
Monitor.Wait(this._lm); //如何理解?
Console.WriteLine("waitpulse1:wakeup");
Console.WriteLine("waitpulse1:result=" + result++ + "thread id" + Thread.CurrentThread.GetHashCode());
Monitor.Pulse(this._lm);//如何理解?
}
Console.WriteLine("waitpulse1:exit thread" + Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._lm);
}
}
public class WaitPulse2
{
private int result = 0;
internal LockMe _lm;
public WaitPulse2(LockMe l)
{
this._lm = l;
}
public void CriticalSection()
{
Monitor.Enter(this._lm);
Console.WriteLine("waitpulse2:entered thread" + Thread.CurrentThread.GetHashCode());
for (int i = 1; i <= 5; i++)
{
Monitor.Pulse(this._lm); //如何理解?
Console.WriteLine("waitpulse2;result= " + result++ + "thread id" + Thread.CurrentThread.GetHashCode());
Monitor.Wait(this._lm); //如何理解?
Console.WriteLine("waitpulse2:wakeup");
}
Console.WriteLine("waitpulse2:exiting thread" + Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._lm);
}
}
public class ClassForMain
{
public static void Main()
{
LockMe l = new LockMe();
WaitPulse1 e1 = new WaitPulse1(l);
WaitPulse2 e2 = new WaitPulse2(l);
Thread t1 = new Thread(new ThreadStart(e1.CriticalSection));
Thread t2 = new Thread(new ThreadStart(e2.CriticalSection));
t1.Start();
t2.Start();
}
}
{
}
public class WaitPulse1
{
private int result = 0;
private LockMe _lm;
public WaitPulse1(LockMe l)
{
this._lm = l; //_lm是LockMe的对象,this._lm中this应该是WaitPulse1吧?这种表示如何理解?
}
public void CriticalSection()
{
Monitor.Enter(this._lm); //此处只写成this不行吗?为什么?
Console.WriteLine("waitpulse1:enter thread" + Thread.CurrentThread.GetHashCode());
for (int i = 1; i < 5; i++)
{
Monitor.Wait(this._lm); //如何理解?
Console.WriteLine("waitpulse1:wakeup");
Console.WriteLine("waitpulse1:result=" + result++ + "thread id" + Thread.CurrentThread.GetHashCode());
Monitor.Pulse(this._lm);//如何理解?
}
Console.WriteLine("waitpulse1:exit thread" + Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._lm);
}
}
public class WaitPulse2
{
private int result = 0;
internal LockMe _lm;
public WaitPulse2(LockMe l)
{
this._lm = l;
}
public void CriticalSection()
{
Monitor.Enter(this._lm);
Console.WriteLine("waitpulse2:entered thread" + Thread.CurrentThread.GetHashCode());
for (int i = 1; i <= 5; i++)
{
Monitor.Pulse(this._lm); //如何理解?
Console.WriteLine("waitpulse2;result= " + result++ + "thread id" + Thread.CurrentThread.GetHashCode());
Monitor.Wait(this._lm); //如何理解?
Console.WriteLine("waitpulse2:wakeup");
}
Console.WriteLine("waitpulse2:exiting thread" + Thread.CurrentThread.GetHashCode());
Monitor.Exit(this._lm);
}
}
public class ClassForMain
{
public static void Main()
{
LockMe l = new LockMe();
WaitPulse1 e1 = new WaitPulse1(l);
WaitPulse2 e2 = new WaitPulse2(l);
Thread t1 = new Thread(new ThreadStart(e1.CriticalSection));
Thread t2 = new Thread(new ThreadStart(e2.CriticalSection));
t1.Start();
t2.Start();
}
}
看看Monitor的帮助和资料 应该就理解了
多线称同步的几个方法中 Monitor 是比较常用的一个
Thread.Sleep(1);
oThread.Abort();
oThread.Join(); //请问这是什么意思?
Console.WriteLine("Alpha.Beta has finished");
oThread.Abort();
oThread.Join(); //请问这是什么意思?
Console.WriteLine("Alpha.Beta has finished");
这个Join()的意思是当前线程停止并等待oThread这个线程结束。
这个Join()还有另外一个重载的版本:
Join(int time);
当前线程停止指定的时间,如果oThread没有停止,则当前线程继续向下执行。
Thread2.Join()
指停止当前的本线程Thread1,然后调用Thread2 , 直到Thread2结束才继续执行Thread1.
Thread2.Join(int time)是指调用Thread2,直到Thread结束或等到int time到了,才回到Thread1.
这个跟Thread.Sleep(int time)是有所差别的,一个是异步,一个是同步.
sleep是无须等待,两个线程一起并发的.而join是有所等待的Join少用为好,如果thread2 还有Thread3.Join的话
容易造成死锁.
Understand?
HOHO,Join的用途在我看来只用于结束线程而绝不是正常的业务逻辑中使用。从这个角度,你非得使用join不可。
比如:
某个通信组件有接收、发送、缓冲处理等多个线程,如果你要求重启这个组件(不是重新建立,是关闭后重新建立连接、缓冲等),那么你就要关闭这些线程,当你的主线程接到关闭的指示之后,就要执行:
oThreadReceive.Abort();//通知其中止
oThreadReceove.Join(3000);//等待其中止已确认这一点,否则再次重新启动这个线程时可能会发生异常。