using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace RingBuffer { internal class Program { static void Main(string[] args) { var queue = new LockFreeRingBuffer(10000); int hasData = 1000; int shouldContinue = 1000; Task.Factory.StartNew(async () => { // 生产者线程 while (hasData>0) { if (!queue.TryEnqueue(hasData)) { Thread.Sleep(1); // 队列满时短暂等待 } Console.WriteLine("生产者添加了{0}", hasData); // 模拟生产延迟 await Task.Delay(10); hasData--; } }); Task.Factory.StartNew(async () => { // 消费者线程 while (shouldContinue > 0) { if (queue.TryDequeue(out var item)) { Console.WriteLine("消费者处理了{0}", item); // 模拟生产延迟 await Task.Delay(10); } } }); Console.ReadLine(); } } }