码迷,mamicode.com
首页 > 其他好文 > 详细

利用BlockingCollection实现生产者和消费者队列,实现写文本

时间:2018-01-23 01:08:34      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:cto   reac   encoding   输出   rect   lock   gen   each   static   

  最近开发几个小项目,需要把结果写到txt文件里面,并且按照时间进行分文件,由于对于效率要求较高,所以采用 生产者和消费者 模型来进行写出文本,线程中只需要添加队列就立即返回,而不需要等待写文件的时间

public class WriteItem : IDisposable
    {
        public string Filename { get; }
        public Encoding Encode { get; }
        public bool Append { get; }
        public bool TimeName { get; }

        private StreamWriter _writer;

        private readonly BlockingCollection<string> _blocking = new BlockingCollection<string>();

        public void Write(string msg)
        {
            _blocking.Add(msg);
        }

        public void WriteLine(string msg)
        {
            Write(msg + Environment.NewLine);
        }

        public WriteItem(string filename, Encoding encode, bool append = true, bool timeName = false)
        {
            Filename = filename;
            Encode = encode;
            Append = append;
            TimeName = timeName;

            if (timeName && string.IsNullOrEmpty(Path.GetExtension(filename)))
            {
                this.Filename = Path.Combine(this.Filename, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
            }
            var dir = Path.GetDirectoryName(this.Filename);
            Directory.CreateDirectory(dir ?? throw new InvalidOperationException());
            _writer = new StreamWriter(this.Filename, this.Append, this.Encode) { AutoFlush = true };
            Task.Factory.StartNew(() =>
            {
                foreach (var s in _blocking.GetConsumingEnumerable())
                {
                    if (TimeName)
                    {
                        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
                        var nowDay = DateTime.Now.ToString("yyyy-MM-dd").ToString();
                        if (fileNameWithoutExtension != nowDay)
                        {
                            _writer.Dispose();
                            _writer = new StreamWriter(this.Filename, this.Append, this.Encode) { AutoFlush = true };
                        }
                    }
                    _writer.Write(s);
                }

            }, TaskCreationOptions.LongRunning);
        }

        public void Dispose()
        {
            _writer?.Dispose();
            _blocking?.Dispose();
        }
    }

  然后再写了个字典来维护:

 public class FileWriteQueue
    {
        private static readonly Dictionary<string, WriteItem> Dictionary = new Dictionary<string, WriteItem>();

        public static void AddOrUpdate(string key, WriteItem item)
        {
            if (Dictionary.ContainsKey(key))
            {
                Dictionary[key].Dispose();
                Dictionary[key] = item;
            }
            else
            {
                Dictionary.Add(key, item);
            }
        }

        public static WriteItem Get(string key)
        {
            return Dictionary[key];
        }
    }

  在实际使用添加WirteItem,设置好输出目录就行了:

            FileWriteQueue.AddOrUpdate("success",new WriteItem(Path.Combine("结果","成功"),Encoding.Default,true,true));
            FileWriteQueue.AddOrUpdate("error", new WriteItem(Path.Combine("结果", "失败"), Encoding.Default, true, true));
            for (int i = 0; i < 1000; i++)
            {
                FileWriteQueue.Get("success").WriteLine(i.ToString());
            }

 

利用BlockingCollection实现生产者和消费者队列,实现写文本

标签:cto   reac   encoding   输出   rect   lock   gen   each   static   

原文地址:https://www.cnblogs.com/mldcy/p/8331619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!