天下之事,必先处之难,而后易之。

C#一个简单的队列测试-生产消费

对于消费者和生产者而言,资源的有无是可见的,至少生产者有了产品之后就会通知消费者去获取。

队列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVParser
{
    /// <summary>
    /// 音视频数据队列
    /// </summary>
    public class AVQueue
    {
       

        private  System.Collections.Concurrent.ConcurrentQueue<AV> mQueues = new System.Collections.Concurrent.ConcurrentQueue<AV>();

        /// <summary>
        /// 队列添加音视频数据
        /// </summary>
        /// <param name="av"></param>
        public  void Add2Queues(AV av)
        {
            mQueues.Enqueue(av);
        }

        /// <summary>
        /// 从队列提取一个音视频
        /// </summary>
        /// <returns></returns>
        public AV GetAVFromQueue()
        {
            AV av;
            var flag = mQueues.TryDequeue(out av);
            if (!flag)
            {
                Console.WriteLine("队列取出数据失败");
            }

            return av;
        }

        /// <summary>
        /// 队列对象个数统计
        /// </summary>
        /// <returns></returns>
        public int CountQueue()
        {
            return mQueues.Count;
        }
    }
}

测试

下面用lock(object){}来同步:

using AVParser.Parser;
using FFmpeg.AutoGen;
using JX;
using RTForwardServer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AVParser
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new FrmPlayer());

            AVQueue aVQueue = new AVQueue();
            int index = 0;
            byte[] bytes = new byte[] { 0x0001 , 0x0002, 0x0003, 0x0004, 0x0005 , 0x0006 , 0x0007 , 0x0008 , 0x0009 , 0x0010 };
            Thread producer = new Thread(() =>
            {
                while (true && index < 10)
                {
                    if (aVQueue.CountQueue()==0)
                    {
                        lock (aVQueue)
                        {
                            AV av = new AV(AV.AVTYPE.MEDIA_AUDIO, new JTRTHead(), new byte[1] { bytes[index] });
                            aVQueue.Add2Queues(av);
                            index++;
                            Console.WriteLine("生产:" + index);
                            Thread.Sleep(1000);
                        }
                    }
                }
            });
            producer.Start();

            int count = 0;
            Thread customer = new Thread(() =>
            {
                while (true&& count<10)
                {
                    if (aVQueue.CountQueue() > 0)
                    {
                        lock (aVQueue)
                        {
                            AV av = aVQueue.GetAVFromQueue();
                            Console.WriteLine("消费:" + BitConverter.ToString(av.Data));
                            count++;
                        }
                            
                    }

                }
            });
            customer.Start();


            Console.ReadLine();
        }
    }
}

输出

生产:1
消费:01
生产:2
消费:02
生产:3
消费:03
生产:4
消费:04
生产:5
消费:05
生产:6
消费:06
生产:7
消费:07
生产:8
消费:08
生产:9
消费:09
生产:10
消费:10

问题

两个线程,一个负责生产一个负责消费,但是生产不阻塞消费,即实现边生产边消费,队列和线程应注意些什么?

posted @ 2024-10-15 17:31  boonya  阅读(28)  评论(0)    收藏  举报  来源
我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。