C# 多线程+队列处理大批量数据,进而缩短处理时间

7 篇文章 0 订阅

转载连接: https://blog.csdn.net/wangtinglong1/article/details/78890645

public void DealData(){

                int pageSize = 200;

//创建队列

                        var queue = new MessageQueueManager<Model>();

                        queue.Setup();
                        //开启生产者线程
                        var producerManager = new TaskManager();
                        producerManager.Setup(5);
                        producerManager.Start((index) =>
                        {
                            var pageIndex = Convert.ToInt32(index) + 1;
                            while (true)
                            {
                                try
                                {
                                    var modelList = GetModelList(pageIndex,pageSize);

                                    if (modelList == null)
                                    {
                                        break;
                                    }

                                    //循环学习统计加入消费者队列
                                    foreach (var model in modelList )
                                    {
                                        //自旋
                                        while (!queue.IsCanEnqueue)
                                        {
                                            Thread.Sleep(5 * 1000);
                                        }
                                        queue.Enqueue(model);
                                    }
                                    if (modelList.Count < pageSize)
                                    {
                                        break;
                                    }
                                    pageIndex += taskCount;
                                }
                                catch (Exception ex)
                                {
                                    pageIndex += taskCount;
                                    if (pageIndex > 2000)
                                    {
                                        break;
                                    }
                                    continue;
                                }
                            }
                        });


                        //消费者线程
                        var consumerManager = new TaskManager();
                        //这里设置一个线程进行处理数据
                        consumerManager.Setup(1);
                        consumerManager.Start(() =>
                        {
                            //从队列中取出数据进行处理
                            queue.Dequeue((info) =>
                            {
                                try
                                {
   //处理数据

                                   // AddOrUpdate( info);
                                }
                                catch (Exception ex)
                                {
                                    
                                }
                            });
                        });


                        producerManager.Wait();
                        queue.Cancel();
                        consumerManager.Wait();

}

/// <summary>
    /// 消息器
    /// </summary>
    public class MessageQueueManager<T>
    {
        private readonly BlockingCollection<T> _messageCollection = new BlockingCollection<T>(10000);


        private CancellationTokenSource _cancellationTokenSource;
        private CancellationToken _cancellationToken;


        public void Enqueue(T message)
        {
            if (_messageCollection.Count < _messageCollection.BoundedCapacity)
                _messageCollection.Add(message);
            if (_cancellationToken.IsCancellationRequested && !_messageCollection.IsCompleted)
                _messageCollection.CompleteAdding();
        }


        public void Dequeue(Action<T> writeAction)
        {
            foreach (var message in _messageCollection.GetConsumingEnumerable())
            {
                writeAction(message);
                Thread.Sleep(10);
            }
        }


        public void Setup()
        {
            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken = _cancellationTokenSource.Token;
        }


        public void Cancel()
        {
            if (!_cancellationTokenSource.IsCancellationRequested)
                _cancellationTokenSource.Cancel(false);
            if (!_messageCollection.IsCompleted) _messageCollection.CompleteAdding();
        }


        public bool IsCanEnqueue
        {
            get
            {
                return _messageCollection.BoundedCapacity > _messageCollection.Count;
            }
        }
    }

//线程管理器

 public class TaskManager
    {
        public List<Task> _taskList;
        public int _total;


        public void Setup(int count)
        {
            _total = count;
            _taskList = new List<Task>(count);
        }


        public void Start(Action doing)
        {
            for (int i = 0; i < _total; i++)
            {
                var task = Task.Factory.StartNew(doing);
                _taskList.Add(task);
            }
        }


        public void Start(Action<int> doing)
        {
            for (int i = 0; i < _total; i++)
            {
                var task = Task.Factory.StartNew((index) => doing((int)index), i);
                _taskList.Add(task);
            }
        }


        public void Wait()
        {
            Task.WaitAll(_taskList.ToArray());
        }
    }
--------------------- 
作者:wangtinglong1 
来源:CSDN 
原文:https://blog.csdn.net/wangtinglong1/article/details/78890645 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值