日期:2014-05-18 浏览次数:20832 次
static void Main(string[] args) { List<Thread> threadlist = new List<Thread>(); for (int i = 0; i < 5; i++) { Thread th = new Thread(new ParameterizedThreadStart(Func)); threadlist.Add(th); } isStop = false; for (int i = 0; i < threadlist.Count; i++) { threadlist[i].Start(i); Console.WriteLine("线程"+i.ToString()+"已经启动..."); } Timer timer = new Timer(new TimerCallback(timerFun),null,0,1000); } static bool isStop = true; static Queue<string> queue = new Queue<string>(); static void Func(object obj) { int index = (int)obj; while (!isStop) { lock (queue) { queue.Enqueue("现在时间" + DateTime.Now.ToString()); } Thread.Sleep(1000); } } static void timerFun(object obj) { lock (queue) { if (queue.Count > 0) { string s = queue.Dequeue(); Console.WriteLine("取出消息:" + s); } } }