有个 thread safe 的queue public class ProducerConsumerQueue
{
private object lockObject = new Object();
private Queue queue = new Queue(); /// <summary>
/// Removes and returns the object at the beginning of the queue.
/// </summary>
/// <returns>
/// The object at the beginning of the queue.
/// </returns>
public object Dequeue()
{
lock (lockObject)
{
while (queue.Count == 0)
{
if (WaitUntilInterrupted())
{
return null;
}
} return queue.Dequeue();
}
} private bool WaitUntilInterrupted()
{
try
{
Monitor.Wait(lockObject);//right here
}
catch (ThreadInterruptedException)
{
return true;
} return false;
}这个monitor.wait大家给讲讲
{
private object lockObject = new Object();
private Queue queue = new Queue(); /// <summary>
/// Removes and returns the object at the beginning of the queue.
/// </summary>
/// <returns>
/// The object at the beginning of the queue.
/// </returns>
public object Dequeue()
{
lock (lockObject)
{
while (queue.Count == 0)
{
if (WaitUntilInterrupted())
{
return null;
}
} return queue.Dequeue();
}
} private bool WaitUntilInterrupted()
{
try
{
Monitor.Wait(lockObject);//right here
}
catch (ThreadInterruptedException)
{
return true;
} return false;
}这个monitor.wait大家给讲讲
Queue oQueue=new Queue();
......
Monitor.Enter(oQueue);
......//现在oQueue对象只能被当前线程操纵了
Monitor.Exit(oQueue);//释放锁
如上所示,当一个线程调用Monitor.Enter()方法锁定一个对象时,这个对象就归它所有了,其它线程想要访问这个对象,只有等待它使用Monitor.Exit()方法释放锁。为了保证线程最终都能释放锁,你可以把Monitor.Exit()方法写在try-catch-finally结构中的finally代码块里。对于任何一个被Monitor锁定的对象,内存中都保存着与它相关的一些信息,其一是现在持有锁的线程的引用,其二是一个预备队列,队列中保存了已经准备好获取锁的线程,其三是一个等待队列,队列中保存着当前正在等待这个对象状态改变的队列的引用。当拥有对象锁的线程准备释放锁时,它使用Monitor.Pulse()方法通知等待队列中的第一个线程,于是该线程被转移到预备队列中,当对象锁被释放时,在预备队列中的线程可以立即获得对象锁。 下面是一个展示如何使用lock关键字和Monitor类来实现线程的同步和通讯的例子,也是一个典型的生产者与消费者问题。这个例程中,生产者线程和消费者线程是交替进行的,生产者写入一个数,消费者立即读取并且显示,我将在注释中介绍该程序的精要所在。用到的系统命名空间如下:using System;
using System.Threading;
首先,我们定义一个被操作的对象的类Cell,在这个类里,有两个方法:ReadFromCell()和WriteToCell。消费者线程将调用ReadFromCell()读取cellContents的内容并且显示出来,生产者进程将调用WriteToCell()方法向cellContents写入数据。public class Cell
{
int cellContents; // Cell对象里边的内容
bool readerFlag = false; // 状态标志,为true时可以读取,为false则正在写入
public int ReadFromCell( )
{
lock(this) // Lock关键字保证了什么,请大家看前面对lock的介绍
{
if (!readerFlag)//如果现在不可读取
{
try
{
file://等待WriteToCell方法中调用Monitor.Pulse()方法
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume: {0}",cellContents);
readerFlag = false; file://重置readerFlag标志,表示消费行为已经完成
Monitor.Pulse(this); file://通知WriteToCell()方法(该方法在另外一个线程中执行,等待中)
}
return cellContents;
} public void WriteToCell(int n)
{
lock(this)
{
if (readerFlag)
{
try
{
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
file://当同步方法(指Monitor类除Enter之外的方法)在非同步的代码区被调用
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
file://当线程在等待状态的时候中止
Console.WriteLine(e);
}
}
cellContents = n;
Console.WriteLine("Produce: {0}",cellContents);
readerFlag = true;
Monitor.Pulse(this); file://通知另外一个线程中正在等待的ReadFromCell()方法
}
}
}
下面定义生产者CellProd和消费者类CellCons,它们都只有一个方法ThreadRun(),以便在Main()函数中提供给线程的ThreadStart代理对象,作为线程的入口。public class CellProd
{
Cell cell; // 被操作的Cell对象
int quantity = 1; // 生产者生产次数,初始化为1 public CellProd(Cell box, int request)
{
//构造函数
cell = box;
quantity = request;
}
public void ThreadRun( )
{
for(int looper=1; looper<=quantity; looper++)
cell.WriteToCell(looper); file://生产者向操作对象写入信息
}
} public class CellCons
{
Cell cell;
int quantity = 1; public CellCons(Cell box, int request)
{
cell = box;
quantity = request;
}
public void ThreadRun( )
{
int valReturned;
for(int looper=1; looper<=quantity; looper++)
valReturned=cell.ReadFromCell( );//消费者从操作对象中读取信息
}
}
然后在下面这个类MonitorSample的Main()函数中我们要做的就是创建两个线程分别作为生产者和消费者,使用CellProd.ThreadRun()方法和CellCons.ThreadRun()方法对同一个Cell对象进行操作。public class MonitorSample
{
public static void Main(String[] args)
{
int result = 0; file://一个标志位,如果是0表示程序没有出错,如果是1表明有错误发生
Cell cell = new Cell( ); //下面使用cell初始化CellProd和CellCons两个类,生产和消费次数均为20次
CellProd prod = new CellProd(cell, 20);
CellCons cons = new CellCons(cell, 20); Thread producer = new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer = new Thread(new ThreadStart(cons.ThreadRun));
//生产者线程和消费者线程都已经被创建,但是没有开始执行 try
{
producer.Start( );
consumer.Start( ); producer.Join( );
consumer.Join( );
Console.ReadLine();
}
catch (ThreadStateException e)
{
file://当线程因为所处状态的原因而不能执行被请求的操作
Console.WriteLine(e);
result = 1;
}
catch (ThreadInterruptedException e)
{
file://当线程在等待状态的时候中止
Console.WriteLine(e);
result = 1;
}
//尽管Main()函数没有返回值,但下面这条语句可以向父进程返回执行结果
Environment.ExitCode = result;
}
}
大家可以看到,在上面的例程中,同步是通过等待Monitor.Pulse()来完成的。首先生产者生产了一个值,而同一时刻消费者处于等待状态,直到收到生产者的“脉冲(Pulse)”通知它生产已经完成,此后消费者进入消费状态,而生产者开始等待消费者完成操作后将调用Monitor.Pulese()发出的“脉冲”。它的执行结果很简单:Produce: 1
Consume: 1
Produce: 2
Consume: 2
Produce: 3
Consume: 3
...
...
Produce: 20
Consume: 20
事实上,这个简单的例子已经帮助我们解决了多线程应用程序中可能出现的大问题,只要领悟了解决线程间冲突的基本方法,很容易把它应用到比较复杂的程序中去。引用别人的啊
如果你使用了Monitor.Wait,调用的线程将阻止在这里,知道另外一个线程调用Monitor.Pulse.代码再继续往下执行
private static object lockObject = new object();
static void Main(string[] args)
{
Console.WriteLine("主线程运行,线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());
Thread t = new Thread(DoWork);
t.Start();
Monitor.Enter(lockObject);
Console.WriteLine("主线程得到锁,开始运行"); Console.WriteLine("主线程释放锁,开始等待");
Monitor.Wait(lockObject);
Console.WriteLine("主线程重新得到锁,结束等待");//这句得等到worker线程pulse之后才能执行
Console.Read();
} static void DoWork()
{
Console.WriteLine("worker线程运行,线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());
Console.WriteLine("停顿10秒");
int i = 1;
while (i <= 10)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
i++;
if (i == 5)
{
Monitor.Enter(lockObject);
Monitor.Pulse(lockObject);
Monitor.Exit(lockObject);
}
} }
class Program
{
private static object lockObject = new object(); static void Main(string[] args)
{
Console.WriteLine("主线程运行,线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());
Thread t = new Thread(DoWork);
t.Start();
Monitor.Enter(lockObject);
Console.WriteLine("主线程得到锁,开始运行");
Thread.Sleep(10000);//这里主线程等待10秒 其目的有2 其一是为了让人看到调用了 t.start()方法后t线程的运行 其二是为了说明 主线程调用了enter方法后 t线程如果想使用lockobject对象 则必须等主线程释放lockobject后
Console.WriteLine("主线程释放锁,开始等待");
Monitor.Wait(lockObject);//释放对象并且进入等待队列 此时主线程进入等待状态(不会向下运行了)
// Console.WriteLine(Thread.CurrentThread.ThreadState);
Console.WriteLine("主线程重新得到锁,结束等待");//这句得等到worker线程pulse之后才能执行
Thread.Sleep(3000);//这里休息3秒 也是为了说明t 线程正在运行
// t.Join();等待t线程运行完毕在执行主线程
Console.WriteLine("主线程运行基本完毕");
// Thread.CurrentThread.Abort(); 如果主线程结束了 那么其子线程也会结束
Console.Read();
} static void DoWork()
{
Console.WriteLine("worker线程运行,线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());
Console.WriteLine("停顿10秒");
int i = 1;
while (i <= 10)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
i++;
if (i == 5)
{
Monitor.Enter(lockObject);
Monitor.Pulse(lockObject);//如果这里没有此方法的调用 虽然 随后调用了exit方法来释放对象 但是未通知等待队列的线程进入就绪队列(本里就是main方法的主线程,如果进入不到等待队列那么就无法获取资源从而运行其线程)
Console.WriteLine("就要释放锁定的对象了");
Monitor.Exit(lockObject);//如果没有此方法释放对象 那么主线程还是无法得到对象从而运行下去
}
} }
/*
*
*总的来说多线程 关于锁定对象的运行程序 的执行步骤 都是围绕着 锁定的对象的使用问题来运行的
* 因为某个对象而阻塞(停止线程的运行 例如wait方法)如果想重新 运行起来则还得应为此对象的释放 才能重新运行
* 如果一个线程因为对象的锁定 而暂停的话 还得等这个对象释放 获取这个对象后才能运行 例如 一个线程调用 enter方法后锁定对象后 另一个线程也调用enter则必须等对象释放后才能继续运行
*/
}