using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Text; namespace TaskList { public class Tasks { private static Queue m_List; //线程互斥 private static object m_obj = new object(); /// /// 初始化队列 /// public Tasks() { if(m_List == null) m_List = new Queue(); } /// /// 线程工作的函数 /// public void ThreadWork() { while (true) { //获取任务 Action work = Pop(); //执行任务 work(); Thread.Sleep(1); } } /// /// 从任务队列中取出任务 /// /// public Action Pop() { Monitor.Enter(m_obj); Action ac = null; try { //当队列有数据,出队.否则等待 if (m_List.Count > 0) { ac = m_List.Dequeue(); } else { Monitor.Wait(m_obj); ac = m_List.Dequeue(); } } finally { Monitor.Exit(m_obj); } return ac; } /// /// 把任务加入任务队列 /// public void Push() { Work w = new Work(); //上锁 Monitor.Enter(m_obj); //委托 Action action = new Action(w.RunWork); //把任务加入队列中 m_List.Enqueue(action); //通知等待队列中的线程锁定对象状态的更改。 Monitor.Pulse(m_obj); Monitor.Exit(m_obj); } } public class Work { private static int number; /// /// 工作函数 /// public void RunWork() { number++; Console.WriteLine("hello world:" + number.ToString()); } } public class Program { public static void Main() { //加入任务 for (int i = 0; i < 200; i++) { Tasks tast = new Tasks(); tast.Push(); } //开启线程来完成执行任务队列中的任务 for (int i = 0; i < 2; i++) { Tasks t = new Tasks(); Thread th = new Thread(new ThreadStart(t.ThreadWork)); th.Start(); } Console.Read(); } } }