Task+ConcurrentQueue+Parallel多线程编程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using Common.Tool.Net;
using Common.Tool;

namespace ConsoleApplication1
{
    class Program
    {
        static string path = "D://Logs//" + DateTime.Now.ToString("yyyy-MM-dd") + "//";
        static ConcurrentQueue<int> queue = new ConcurrentQueue<int>();
        static void Main(string[] args)
        {
            TaskHelper.GetTaskInitialBasics<int>(path, queue, IntoQueue, ScanQueue);
        }

        private static void IntoQueue(CancellationToken ct)
        {
            Console.WriteLine("入队线程启动");
            List<int> list = new List<int>();

            for (int i = 1; i <= 10000; i++)
            {
                list.Add(i);
            }

            //使用并行迭代提升速度           
            Parallel.ForEach(list, new ParallelOptions { MaxDegreeOfParallelism = 500 }, l =>
            {
                //入队操作
                queue.Enqueue(l);
            });
            Console.WriteLine("入队线程结束");
            Utils.WriteLog(path, "===========入队线程结束!===========", "Result", false, true);
        }

        private static void ScanQueue(CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            Console.WriteLine("出队线程启动");
            while (true)
            {
                if (!queue.IsEmpty)
                {
                    //从队列中取出  
                    int a = 0;
                    if (queue.TryDequeue(out a))
                    {
                        Console.WriteLine("正在录入数据");
                        Utils.WriteLog(path, a.ToString(), "demo", false, true);
                    }
                }
                else
                {
                    Thread.Sleep(1500);
                }
                Console.WriteLine("队列剩余总数:" + queue.Count);
                ct.ThrowIfCancellationRequested();
            }
        }
    }
}


TaskHelper类


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

namespace Common.Tool.Net
{
    public class TaskHelper
    {
        public delegate void IntoQueueMethod(CancellationToken token);
        public delegate void ScanQueueMethod(CancellationToken token);

        /// <summary>
        /// task初始化基础设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="logPath"></param>
        /// <param name="queue"></param>
        /// <param name="MakeIntoQueueMethod"></param>
        /// <param name="MakeScanQueueMethod"></param>
        public static void GetTaskInitialBasics<T>(string logPath, ConcurrentQueue<T> queue, IntoQueueMethod MakeIntoQueueMethod, ScanQueueMethod MakeScanQueueMethod)
        {
            CancellationTokenSource token = new CancellationTokenSource();

            var task = Task.Factory.StartNew(() => MakeIntoQueueMethod(token.Token));
            var task1 = Task.Factory.StartNew(() => MakeScanQueueMethod(token.Token));

            Thread.Sleep(1000);

            if (task.IsFaulted)
            {
                /*  循环输出异常    */
                foreach (Exception ex in task.Exception.InnerExceptions)
                {
                    Console.WriteLine(ex.Message);
                    Utils.WriteLog(logPath, ex.Message, "IntoQueueError", false, true);
                }
            }

            if (task1.IsFaulted)
            {
                /*  循环输出异常    */
                foreach (Exception ex in task1.Exception.InnerExceptions)
                {
                    Console.WriteLine(ex.Message);
                    Utils.WriteLog(logPath, ex.Message, "ScanQueueError", false, true);
                }
            }

            Task.WaitAll(task); //等待入队任务结束
            Thread.Sleep(5000);
            while (true)
            {
                if (queue.Count == 0 && queue.IsEmpty) //队列为空
                {
                    token.Cancel(); //发送取消任务指令

                    Console.WriteLine("出队线程结束");
                    Utils.WriteLog(logPath, "===========出队线程结束!===========", "Result", false, true);

                    Console.WriteLine("===========全部录入完成!===========");
                    Utils.WriteLog(logPath, "===========全部录入完成!===========", "Result", false, true);
                    break;
                }
            }
            Thread.Sleep(2000);
            Console.WriteLine("若要结束任务请输入T");
            if (Console.ReadLine() == "T")
            {
                task.Dispose();
                task1.Dispose();
            }
        }

    }
}


实验结果



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值