C#处理大批量数据


using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace ConsoleApi
{
     /// <summary>
    /// 数据传输类
    /// </summary>
    public class ResponseModel
    {
        public int Code { get; set; }

        public string Msg { get; set; }

        public string Data { get; set; }
    }

    /// <summary>
    /// 多线程批量数据处理
    /// </summary>
    public class MultithreadBatchDataProcessing
    {
        //线程安全队列
        private ConcurrentQueue<ResponseModel> queue = new ConcurrentQueue<ResponseModel>();
 
        /// <summary>
        /// 模拟设置数据
        /// </summary>
        public void SetData()
        {
            Console.WriteLine($"开始数据设置,时间:{DateTime.Now};");
            for (int i = 0; i < 10000; i++)
            {
                var model = new ResponseModel { Code=i, Msg=$"第{i+1}次循环", Data=$"产生随机数:{new Random().Next(1000,10000)}" };
                queue.Enqueue(model); //模拟数据入队
                Thread.Sleep(1);    //这里是随机数生成时需要
            }
            Console.WriteLine($"10000条数据设置完毕!时间:{DateTime.Now};");
        }
 
        /// <summary>
        /// 多线程处理数据
        /// </summary>
        public void MultitDataProcessing()
        {
            int threadCount = 10; //开启10个线程
            for (int i = 0; i < threadCount; i++)
            {
                string fileName = $"task{i}.txt";
                //开启新线程
                Task.Factory.StartNew(() =>
                {
                    var sb = new StringBuilder();
                    int j = 0;
                    //数据循环出队
                    while (queue.TryDequeue(out ResponseModel model))
                    {
                        //处理数据
                        if (model != null)
                            sb.AppendLine($"==》Code={model.Code},Msg={model.Msg},Data={model.Data}");
 
                        if (j % 100 == 0 || (queue.Count.Equals(0) && j < 100))
                        {   
                            Console.WriteLine($"每100条输出一次控制台,并暂停100毫秒, 第{i}次文件:{fileName}");
                            Console.WriteLine(sb.ToString());
                            sb = new StringBuilder();
                            Thread.Sleep(100);
                        }
                        j++;
                    }
                });
            }
        }
    }
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值