c# queue 实现 队列多线程,具体目标 就是一个方法 在接受输入的字符串的同时 主函数从队列提取,并输出
Q:
其实是想做一个程序 一边接收指令 并将指令存入队列 然后主函数检测队列不为空就取队列中的指令进行处理,处理完再次来取指令
想用c#实现 谁能给个简单的demo 能实现标题的功能就行 其他部分我都做完了 就差这块了
A:
static Queue<string> q = new Queue<string>();
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Thread th = new Thread(new ThreadStart(printQueue));
th.Start();
while (true)
{
q.Enqueue(Console.ReadLine());
}
Console.Read();
}
static private void printQueue()
{
while (true)
{
if (q.Count>0)
Console.WriteLine(q.Dequeue());
}
}
浙公网安备 33010602011771号