Task+ConcurrentQueue+Parallel多线程编程
来源:互联网 发布:针对接口编程 编辑:程序博客网 时间:2024/06/04 19:48
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 0
- Task+ConcurrentQueue+Parallel多线程编程
- thread task parallel plinq async await多线程 任务及异步编程
- Task Parallel.For、Parallel.ForEach、Parallel.Invoke
- 构建多线程应用程序2 Parallel 并行类:Parallel.ForEach()、Task异步操作类:Task.Factory.StartNew()、并行LINQ查询: AsParallel()
- C#的Task多线程编程
- C#多线程编程Task任务
- C# Task.WhenAll Parallel
- C# Task.WhenAll Parallel
- .Net多线程编程—System.Threading.Tasks.Parallel
- Parallel task in C# 4.0
- Task Parallel Library ( TPL)学习
- Oracle Parallel 多线程
- Perl多线程Parallel::ForkManager
- Parallel多线程使用
- Oracle Parallel 多线程
- 线程的理解Task和Parallel
- Parallel 并发编程实例
- C#并行编程-Parallel
- 【转】模仿绘画风格的算法:A Neural Algorithm of Artistic Style
- DPM代码(C++)
- 离散正(余)弦信号的时域与FFT变换后所得频域之间的关系(幅值和相角)
- layui结合form,table的全选、反选v1.0
- mobiscroll 插件札记
- Task+ConcurrentQueue+Parallel多线程编程
- git push -u orgin master报错
- java生成两种二维码
- uC/OS-II源码解析(os_q.c)
- SharePoint Framework 企业向导(三)
- springboot整合多个activemq
- SharePoint Framework 企业向导(四)
- Collections排序List<Map>
- Kafka 设计与原理详解