<span style="font-weight: bold;">下边代码解决了 避免创建新线程 规避了线程池的创建过多线程 解决浪费资源问题 </span>
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace MyThreadPool { /// <summary> /// 线程任务队列 /// </summary> /// <typeparam name="T"></typeparam> public class BackgroundTasks<T> { /// <summary> /// 带参数不带返回值的函数委托 /// </summary> private Action<T> Function; /// <summary> /// 先进先出队列 /// </summary> private Queue<T> list = new Queue<T>(); /// <summary> /// 构造函数 /// </summary> /// <param name="fun"></param> public BackgroundTasks(Action<T> fun) { this.Function = fun; Thread th = new Thread(Run); th.IsBackground = true; th.Start(); } /// <summary> /// 执行线程 方法队列 /// </summary> private void Run() { while (true) { if (list.Count == 0) { Thread.Sleep(10); } else { T data; lock (list) { data = list.Dequeue(); } try { Function(data); } catch { } Thread.Sleep(10); } } } /// <summary> /// 添加队列到线程中 /// </summary> /// <param name="data"></param> public void Add(T data) { lock (list) { list.Enqueue(data); } } } }
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace MyThreadPool { class Program { static void Main(string[] args) { BackgroundTasks.Add((obj) => { Console.WriteLine("这个任务的添加时间是1:{0}", obj.ToString()); },DateTime.Now.Millisecond); BackgroundTasks.Add((obj) => { Console.WriteLine("这个任务的添加时间是2:{0}", obj.ToString()); }, DateTime.Now.Millisecond); BackgroundTasks.Add((obj) => { Console.WriteLine("这个任务的添加时间是3:{0}", obj.ToString()); }, DateTime.Now.Millisecond); BackgroundTasks.Add((obj) => { Console.WriteLine("这个任务的添加时间是4:{0}", obj.ToString()); }, DateTime.Now.Millisecond); ///自定义线程队列 传入泛型参数,只创建一个线程 BackgroundTasks<DateTime> task = new BackgroundTasks<DateTime>(obj => { Console.WriteLine("这个任务的添加时间是5:{0}", obj.ToString()); }); task.Add(DateTime.Now); ///线程池的 方法 每次调用都创建新线程 ThreadPool.SetMaxThreads(100, 1000); ThreadPool.QueueUserWorkItem((obj) => { Console.WriteLine("这个任务的添加时间是6:{0}", obj.ToString()); }, DateTime.Now); Console.Read(); } } /// <summary> /// 线程任务队列 /// </summary> public class BackgroundTasks { /// <summary> /// 任务实体 /// </summary> private class TaskEntity { public Action<object> Function; public object Data; public TaskEntity(Action<object> func, object data) { this.Function = func; this.Data = data; } } /// <summary> /// 先进先出队列 /// </summary> static Queue<TaskEntity> list = new Queue<TaskEntity>(); /// <summary> /// 构造函数 创建线程 /// </summary> static BackgroundTasks() { Thread thread = new Thread(Run); thread.IsBackground = true; thread.Start(); } /// <summary> /// 执行线程 方法队列 /// </summary> static void Run() { while (true) { if (list.Count == 0) { Thread.Sleep(1000); } else { TaskEntity entity; lock (list) { entity = list.Dequeue(); } try { entity.Function(entity.Data); } catch {} Thread.Sleep(10); } } } /// <summary> /// 添加任务队列 /// </summary> /// <param name="func"></param> /// <param name="data"></param> public static void Add(Action<object> func, object data) { lock (list) { list.Enqueue(new TaskEntity(func, data)); } } } }
由于队列中的任务是单线程执行,可能会导致某些任务在很长时间后才会被执行到,或者重启IIS导致很多任务还没有被执行就被丢弃。无论怎么,这种设计还是适用于很多“一般情况”。
时间: 2024-10-03 16:48:40